无法读取 DocumentDB 中未定义的 属性“_self”

Cannot read property '_self' of undefined in DocumentDB

我想使用 Azure 在 DocumentDB 中创建一个集合。我写了一段代码,但在执行时会抛出一个错误,指出 "Cannot read property '_self' of undefined"。 下面是我的代码,任何人都可以看看我的代码并帮助我。

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, 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"}' }

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

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

    与:

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

  2. 公开 post 您的授权密钥很危险 - 因为现在任何人都可以访问您的数据库。我强烈建议重新生成密钥;从 Whosebug 中删除它是不够的。

  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'")

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