【问题标题】:Cannot read property '_self' of undefined in DocumentDB无法读取 DocumentDB 中未定义的属性“_self”
【发布时间】:2015-05-05 14:14:55
【问题描述】:

我想使用 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)

【问题讨论】:

  • 在 client.createDatabase 的回调中,在 client.createCollection 之前你尝试过 console.log(err); ?
  • 是的,我试过了,但没用
  • 错误:D:\Node.js\azure\nodetest>node app.js { 代码:401,正文:'{"code":"Unauthorized","message":"必需的标头授权正在丢失。确保传递了有效的授权令牌。\\r\\nActivityId: a98d9f51-982 a-450d-8bc1-f1a0ce5c7eb2"}' } D:\Node.js\azure\nodetest\app.js:21
  • 第一件事:您应该立即重新生成数据库的访问密钥。并且永远不要再公开发布它。使用该密钥,任何人都可以访问您的数据库帐户!!!
  • 是的,我已经更改了我的数据库访问密钥,它抛出了同样的错误

标签: node.js azure azure-cosmosdb


【解决方案1】:

这里有一些提示:

  1. 查看您得到的异常:

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

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

    { code: 401, body: '{"code":"Unauthorized","message":"Required Header authorization is miss ing. Ensure a valid Authorization token is passed.\\r\\nActivityId: a98d9f51-982 a-450d-8bc1-f1a0ce5c7eb2"}' }

    错误消息表明客户端未能使用您的 authkey 签署数据库请求。查看您的代码,客户端需要masterKey 属性(注意骆驼大小写)而不是masterkey。替换以下字符串将修复您的代码:

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

    与:

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

  2. 公开发布您的身份验证密钥很危险 - 因为现在任何人都可以访问您的数据库。我强烈建议重新生成密钥;从 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'")

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多