Search code examples
meteormeteor-blaze

Return _id to client on insert in Meteor


Whats the best way to retrieve the _id of the data thats JUST been inserted into a collection to the client. I'm trying to redirect on submit with FlowRouter.go("/:_id") but there is no way of retrieving the current id.

Meteor.methods ({
    insertData: function(data) { 
        Events.insert(data);
// somehow return this insert's _id to client
    }
});

What is the best way of retrieving this data on the client side as soon as its submitted into the database.


Solution

  • Server:

    Meteor.methods ({
        insertData: function(data) { 
          return Events.insert(data);
        }
    });
    

    Client:

      Meteor.call('insertData', (err, response) => {
          if (err) {
            console.log(err.reason);
          } else if (response) {
            console.log("ID: ", response);
          }
        });