node.js - 无法读取 DocumentDB 中未定义的属性 '_self'

标签 node.js azure azure-cosmosdb

我想使用 Azure 在 DocumentDB 中创建集合。我写了一个代码,但在执行时会抛出一个错误,指出“无法读取未定义的属性‘_self’”。 下面是我的代码,请任何人查看我的代码并帮助我。

app.js

var DocumentClient = require("documentdb").DocumentClient;

//the URI value from the DocumentDB Keys blade on http://portal.azure.com 

var endpoint ="https://somedbname.documents.azure.com:443/";

//the PRIMARY KEY value from the DocumentDB Keys blade on 

http://portal.azure.com 

var authkey = 

"SomeAuthKey=="; 

var client = new DocumentClient(endpoint,{"masterkey": authkey});

var databaseDefinition = {"id": "documentdb1"};

var collectionDefinition = {"id": "table1"};

var documentDefinition = {

        "id": "pgogula",

        "stuff": "Hello World",

        "bibbidi": {

        "bobbidi": "boo"

        }
};


client.createDatabase(databaseDefinition, function(err,database){

client.createCollection(database._self,collectionDefinition, 

function(err,collection){

client.createDocument(collection._self, documentDefinition, 

function(err,document){

client.queryDocuments(collection._self,"SELCT * FROM docs d WHERE 

d.bibbidi.bobbidi='boo'").toArray(function(err, results){

 console.log("Query Results:");

 console.log(results);

 });

});

});

});

错误:

D:\Node.js\azure\nodetest>node app.js

D:\Node.js\azure\nodetest\app.js:20

client.createCollection(database._self,collectionDefinition, function(err,coll
                                ^
TypeError: Cannot read property '_self' of undefined

at D:\Node.js\azure\nodetest\app.js:20:33

    at IncomingMessage.<anonymous> (D:\Node.js\azure\nodetest\node_modules\docum

entdb\lib\request.js:49:14)

    at IncomingMessage.emit (events.js:129:20)

    at _stream_readable.js:908:16

    at process._tickCallback (node.js:355:11)

最佳答案

这里有一些提示:

  1. 查看您收到的异常:

    client.createCollection(database._self,collectionDefinition, 函数(err,coll ^ 类型错误:无法读取未定义的属性“_self”

    database 未定义,因为您收到传递到回调的错误。看来您收到的错误消息是:

    { code: 401, body: '{"code":"Unauthorized","message":"缺少必需的 header 授权。确保传递有效的授权 token 。\\r\\nActivityId: a98d9f51-982 a-450d-8bc1-f1a0ce5c7eb2"}' }

    该错误消息表明客户端无法使用您的身份验证 key 对数据库请求进行签名。查看您的代码,客户端需要一个 masterKey 属性(注意驼峰式大小写),而不是 masterkey。替换以下字符串将修复您的代码:

    var client = new DocumentClient(endpoint,{"masterkey": authkey});

    与:

    var client = new DocumentClient(endpoint,{"masterKey": authkey});

  2. 公开发布您的身份验证 key 是危险的 - 因为现在任何人都可以访问您的数据库。我强烈建议重新生成 key ;从 stackoverflow 中删除它还不够。

  3. 您在以下文档查询中存在拼写错误,这将导致查询失败。请替换:

    client.queryDocuments(collection._self,"SELCT * FROM docs d WHERE d.bibbidi.bobbidi='boo'")

    与:

    client.queryDocuments(collection._self,"SELECT * FROM docs d WHERE d.bibbidi.bobbidi='boo'")

这应该能让你的代码正常工作;或者至少在我的电脑上是这样的:)

关于node.js - 无法读取 DocumentDB 中未定义的属性 '_self',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055492/

相关文章:

node.js - 如何在 ES6 中导入传递子类参数的 Node 模块?

node.js - 类型错误 : Socket is not a constructor

azure - 如何使用deployIfNotExists策略强制在Azure存储上配置删除保留?

azure - Azure 中的新资源警报

azure-cosmosdb - 在 Azure Cosmos DB for MongoDB API 中使用无默认索引策略的 sort() 游标方法

powershell - 使用 PowerShell (SQL API) 在 Cosmos DB 中插入文档

node.js - SuperAgent + Node.js 连接被拒绝

php - 无法将 Azure ML API 与 PHP 集成

azure - 使用数据工厂从 cosmos db 作为源进行复制事件时出错

javascript - 使用 Express/Sendgrid 将电子邮件模板用于联系表单