Return the inserted values


#1

Hello,

How do I get inserted values in the collection after using Collection.insert() function ? I want get all the values which has inserted in collection. Is there any way to do so ?

Thanks!


#2

Collection.insert returns the inserted _id, so you can just fetch it afterwards

let insertedId = Posts.insert({title: "new post"});
let insertedDoc = Posts.findOne(insertedId);

The insert will return an id on the client, even if it fails on the server. If you want to catch whether the insert was successfull, use a callback as the second param:

Posts.insert({title: "new post"}, (error, insertedId) => {
  if(error) {
     // error handling
  } else {
    let insertedDoc = Posts.findOne(insertedId);
    ...
  }
  
});