Cannot read property "_self" of undefined in DocumentDB

I want to create a collection in DocumentDB using Azure. I have some code written, but when I execute it, I get the error "Cannot read property" itself "from undefined". Below is my code, can anyone look into my code and help me.

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);

 });

});

});

});

      

Mistake:

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)

      

+3


source to share


1 answer


Here are some tips:

  • Looking at the exception you are getting:

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

    database

    - undefined because you are getting an error being passed to the callback. It looks like you got the error:

    { 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 was unable to sign the database request with your authkey. If you look at your code, the client is expecting a property masterKey

    (note the camel case), not masterKey

    . Replacing the following line will fix your code:

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

    from:

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

  • It is dangerous to publish your out-key publicly - as now any user can access your database. I highly recommend recovering the key; removing it from stackoverflow is not enough.

  • You have a typo in the next document request that will cause the request to fail. Please replace:

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

    from:

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



This should make your code work; or at least it was on my computer :)

+4


source







All Articles