Discover Meteor: return _id or not?


#1

Just a basic question on the Discover Meteor tutorial. I’ve worked through the Microscope exercise but now trying to understand how I replicate in my own project.

I’m comparing the postInsert method…

    var postId = Posts.insert(post);
    return {
        _id: postId
    };

…with the two ways we insert Posts in Fixtures file.

Option#1

    var telescopeId = Posts.insert({...});
    ...
    postId: telescopeId,

Option #2

    Posts.insert({});

These are all slightly different but all seem to work the same way. So my question is, what is the purpose of :

return {
    _id: postId
};

…in the method? The book says “returning the resulting _id to the client (in other words, the original caller of this method) in a JavaScript object”. But if it isn’t necessary in Fixtures and I can still access those documents, why is it necessary? Specifically, is excluding this code the reason I’m having trouble returning the _id from documents when querying on other attributes?

Complete beginner to all programming so forgive the elementary question. Thanks for any help.

Brendan


#2

If you are writing method, it is nice to return something. When you are calling it usually from client, there is callback argument which gets this result and error code. It is nice to have something returned.

And it should not affect any find operation results above collections.

I did not read the book, so dont know all these case uses u refer to.


#3

Thank you very much. All good info.