Friday 9 June 2017

nodejs and mongoose return undefined when logging json data

I have a function to return json data from a mongoose table.It first insert a newpost and then retrive the same post by the post._id.

   dbConfig.newPost(post,id,function (err,post) {
         if(err) throw(err);
         dbConfig.getPostById(post._id,function (err,doc) {
            if(err) throw err;
            var data = JSON.stringify(doc);
             console.log(data);
             console.log(data._id);
         });

     });

The query prints the data variable as a array of objects in JSON but when i try to only print the 'data._id' it returns undefined. Does anyone know what i'm doing wrong? An snip of the json array is below

[{"_id":"593ab877777ae318e4a35637","userPost":"savassge","_creator":{"_id":"592b8c2d0c28f01970ad4df7","username":"adrian"},"__v":0,"dateCreated":"2017-06-09T15:02:15.716Z"}]



via Adrian

No comments:

Post a Comment