Azure Chatbot fails with "SyntaxError: Unexpected identifier - cosmosClient.databases.createIfNotExists" while connecting to CosmosDB

Azure Chatbot fails with "SyntaxError: Unexpected identifier - cosmosClient.databases.createIfNotExists" while connecting to CosmosDB

我对 Azure 机器人服务和整个 Azure 平台非常陌生。 我正在尝试使用 node.js 创建聊天机器人,但在尝试连接到 CosmosDB 时出现以下错误。

在我添加以下代码以连接到 CosmosDB 之前,该机器人 运行 正常。

如有任何帮助或指导,我们将不胜感激!

P.S。 - 我已经添加了“@azure/cosmos”包,如果我只是删除 try-catch 段,代码运行没有任何错误。

连接CosmosDB的代码:

var async=require("async"); 
var await=require("await"); 

const CosmosClientInterface = require("@azure/cosmos").CosmosClient;
const databaseId = "ToDoList"; 
const containerId = "custInfo"; 

const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";

const cosmosClient = new CosmosClientInterface({
    endpoint: endpoint,
    auth: {
      masterKey: authKey
    },
    consistencyLevel: "Session"
  });

async function readDatabase() {
   const { body: databaseDefinition } = await cosmosClient.database(databaseId).read();
   console.log(`Reading database:\n${databaseDefinition.id}\n`);
}

错误信息:

Sat Jan 12 2019 03:40:08 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
    at Module._compile (module.js:570:32)
Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
    at Module._compile (module.js:570:32)

如果不在 async 函数中,您就无法等待。

将所有代码转储到 async function main(){} 方法中,然后调用 main().catch((err) => console.log(err)); 或类似的东西来启动承诺并处理错误。

您可以在此示例中看到这种模式的示例:https://github.com/Azure/azure-cosmos-js/blob/master/samples/ChangeFeed/app.js#L33

--- 编辑 1 ---

这是用 Promises 重写的示例:

const CosmosClientInterface = require("@azure/cosmos").CosmosClient;
const databaseId = "ToDoList"; 
const containerId = "custInfo"; 

const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";

const cosmosClient = new CosmosClientInterface({
    endpoint: endpoint,
    auth: {
      masterKey: authKey
    },
    consistencyLevel: "Session"
  });

cosmosClient.database(databaseId).read().then(({body: databaseDefinition}) => {
   console.log(`Reading database:\n${databaseDefinition.id}\n`);
}).catch((err) {
   console.err("Something went wrong" + err);
});

对于上面的示例,您不需要导入 async/await,它们现在是 JavaScript 中的关键字。

这是一篇比较和对比 Async/Await 和 Promises 的博客 post:https://hackernoon.com/should-i-use-promises-or-async-await-126ab5c98789