Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js & Mongoose, can't recover _id
    primarykey
    data
    text
    <p>I'm trying to save a document in my collection and if the save is successful, return the _id of this same document. The problem is I get an undefined value to my _id in both case, either the created model from mongoose or from the callback return. Basically, my only way of getting the _id would be to search the document by one of its properties, and then get the value. This approach isnt what I want, knowing what im currently trying to do should work.</p> <pre><code>var createTrophy = new Trophy({ name : post.name, accessCode : post.password, description : post.description, members : [id] }); Trophy.findOne({name:post.name}, function(err, trophy) { if(err){ console.log('Mongoose: Error: ' + err); res.send('Error db query -&gt; ' + err); } else if(trophy){ console.log('Trophy ' + trophy.name + ' already existant'); res.send('Trophy ' + trophy.name + ' already existant'); }else{ createTrophy.save(function(err, doc){ var uid = createTrophy._id; if (err) { console.log('Error in trophy saving:' + err); res.send('Error in trophy saving:' + err); }else{ User.findOne({_id:post.id}, function(err, user) { if(err){ console.log('Mongoose: Error: ' + err); res.send('Error db query -&gt; ' + err); } else if(user){ console.log(doc._id + ' ' + uid); user.trophyLink = doc._id; res.send(user); //user.save(function(err){ // if(err){res.send('Couldnt update trophy of profile');} //}); } else{ console.log('User id Inexistant'); res.send('User id Inexistant'); } }); } }); } }); }); </code></pre> <p>The Schema</p> <pre><code> var Trophy = new Schema({ _id : ObjectId, name : String, accessCode : String, description : String, //reference to User ID members : [Number], comments :[Comment] }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload