Reputation: 599
I want to create a collection in DocumentDB by using Azure. I have a written a code but when executing it throws an error saying that "Cannot read property '_self' of undefined". Below is my code,can any one please do look into my code and help me out.
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);
});
});
});
});
Error:
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)
Upvotes: 3
Views: 2118
Reputation: 8119
Here are a few tips:
Looking at the exception you are getting:
client.createCollection(database._self,collectionDefinition, function(err,coll
^
TypeError: Cannot read property '_self' of undefined
database
is undefined because you are getting an error passed in to the callback. Looks like you the error message you are receiving is:
{ 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"}' }
The error message indicates that the client failed to sign the database request with your authkey. Looking at your code, the client expects a masterKey
property (note camel-case) rather than masterkey
. Replacing the following string will fix your code:
var client = new DocumentClient(endpoint,{"masterkey": authkey});
with:
var client = new DocumentClient(endpoint,{"masterKey": authkey});
It's dangerous to post your auth key publicly - as anyone can now access your database. I'd strongly recommend regenerating the key; removing it from stackoverflow isn't enough.
You have a typo in the following document query which will cause the query to fail. Please replace:
client.queryDocuments(collection._self,"SELCT * FROM docs d WHERE d.bibbidi.bobbidi='boo'")
with:
client.queryDocuments(collection._self,"SELECT * FROM docs d WHERE d.bibbidi.bobbidi='boo'")
This should get your code working; or at least it did on my computer :)
Upvotes: 4