JavaScript Azure Function v4 在从 cosmos db 获取记录期间挂起

问题描述 投票:0回答:1

我正在学习 Azure 函数并遇到了问题。 从 cosmos db 获取记录时,该函数挂起。 我使用 cosmos db 模拟器。 该函数无需从 cosmos db 获取记录的代码即可正常工作。我看到日志:

First id value is: 3

const { app } = require('@azure/functions');

app.http('httpTrigger', {
    methods: ['GET', 'POST'],
    handler: async (request, context) => {
        try {
        const Id = request.query.get('id');

        context.log(`First id value is: ${Id}`);

        return {body: `Response id =${Id}`};

    } catch (error) {
        context.log(`Error: ${error}`);
        return { status: 500, body: 'Internal Server Error' };
      }
    }
});

本地.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "CosmosDBConnectionString":"AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
  }
}

但是该函数无法与从 cosmos db 获取记录的代码一起正常工作。我没有看到日志。看起来函数已被触发,但处理程序内的代码(日志)未执行。

const { app, input } = require('@azure/functions');

const cosmosInput = input.cosmosDB({
    
    databaseName: 'my-database',
    containerName: 'my-container',
    connection: 'CosmosDBConnectionString',
    sqlQuery: 'SELECT * FROM c'
});

app.http('httpTrigger', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    extraInputs: [cosmosInput],
    handler: async (request, context) => {
        try {
        const Id = request.query.get('id');

        context.log(`First id value is: ${Id}`);

        const toDoItem = context.extraInputs.get(cosmosInput);

        context.log(`Second id value is: ${Id}`);

        return {body: `Response id =${Id}`};

    } catch (error) {
        context.log(`Error: ${error}`);
        return { status: 500, body: 'Internal Server Error' };
      }
    }
});

我尝试在 Azure 函数中从 cosmos db 获取记录。

javascript azure-functions azure-cosmosdb azure-cosmosdb-emulator
1个回答
0
投票

我尝试在 Azure 函数中从 cosmos db 获取记录。

尝试使用以下代码使用 JavaScript 函数 v4 从 Azure Cosmos DB 模拟器检索数据。

const { app } = require('@azure/functions');
const { CosmosClient } = require("@azure/cosmos");

const cosmosClient = new CosmosClient({ endpoint, key });

app.http('httpTrigger1', {
    methods: ['GET'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log(`Http function processed request for url "${request.url}"`);

        try {
            const database = cosmosClient.database(databaseId);
            const container = database.container(containerId);

            const querySpec = {
                query: "SELECT * FROM c"
            };

            const { resources: items } = await container.items.query(querySpec).fetchAll();
            
            return {
                body: JSON.stringify(items) // Stringify the items array
            };
        } catch (error) {
            context.log.error("Error retrieving data from Cosmos DB:", error);
            return {
                status: 500,
                body: "Internal Server Error"
            };
        }
    }
});

输出: enter image description here

[{"id":"1","name":"Sai","dept":"IT"},{"id":"2","name":"Pavan","dept":"Non-IT"},{"id":"3","name":"Venky","dept":"Hardware"}]
© www.soinside.com 2019 - 2024. All rights reserved.