首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure在连接到SyntaxError时遇到“cosmosClient.databases.createIfNotExists:意外标识符- cosmosClient.databases.createIfNotExists”失败

Azure在连接到SyntaxError时遇到“cosmosClient.databases.createIfNotExists:意外标识符- cosmosClient.databases.createIfNotExists”失败
EN

Stack Overflow用户
提问于 2019-01-10 06:43:28
回答 1查看 440关注 0票数 0

我是一个非常新的Azure机器人服务和整个Azure平台。我正在尝试使用node.js创建一个聊天机器人,但是当我试图连接到CosmosDB时,我得到了下面的错误。

在我添加下面的代码连接到CosmosDB之前,机器人运行得很好。

任何有关这方面的帮助或指导将不胜感激!

P.S. -我添加了'@azure/cosmos‘包,如果我只删除了try-catch段,代码就会运行,没有任何错误。

连接到CosmosDB:的代码

代码语言:javascript
复制
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`);
}

错误消息:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-10 18:44:40

您不能在没有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

下面是你的样本,上面写着承诺:

代码语言:javascript
复制
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);
});

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

下面是一篇比较和对比异步/等待和承诺的博客文章:https://hackernoon.com/should-i-use-promises-or-async-await-126ab5c98789

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54123183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档