Call return function Meteor.call Undefined

I have this code on the client:

var Checklist = {
            title: this.title,
            belongs_to: this.belongs_to,
            type: this.type,
            items: this.items
        };
        Meteor.call(
            'create_checklist',
            Checklist,
            function(error,result){
                console.log('error',error,'result',result);
                // if(!error) {
                //  Router.go('/checklist/'+response);
                // }
            }
        );

      

And this is on the server:

create_checklist: function(Checklist) {
        Checklists.insert(
            {
                title: Checklist.title,
                belongs_to: Checklist.belongs_to,
                type: Checklist.type,
                items: Checklist.items
            }, function(error,id){
                console.log(error,id);
                if(id) {
                    return id;
                } else {
                    return error;
                }
            }
        );
    },

      

Meteor.call successfully sends information to server when checklist is created. I see the ID of the new checklist on the server console. However, the client sees undefined

for both the error and the result.

+3


source to share


3 answers


You are not returning the result to your server-side method. You cannot return values ​​from a callback. Return only the result of Checklists.insert:

create_checklist: function(Checklist) {
        return Checklists.insert(
            {
                title: Checklist.title,
                belongs_to: Checklist.belongs_to,
                type: Checklist.type,
                items: Checklist.items
            }, function(error,id){
                console.log(error,id);
                if(id) {
                    return id;
                } else {
                    return error;
                }
            }
        );
    },

      



According to Meteorite Papers , the insert method returns the ID of the inserted document.

On the server, if you don't provide a callback, insert the blocks until the database recognizes the record, or throws an exception if something goes wrong.

+5


source


You don't need to return anything, replace the meteor method with this.

create_checklist: function(Checklist) {
        Checklists.insert(
            {
                title: Checklist.title,
                belongs_to: Checklist.belongs_to,
                type: Checklist.type,
                items: Checklist.items
            }
        );
    }

      



meteor.call callback

knows how to deal with the server, so you use error result

if something doesn't match the method the server will throw an error and the meteor call will fail.

+1


source


Simplified to a minimum:

create_checklist: function(Checklist) {
  return Checklists.insert(Checklist); 
}

      

The client code should see the _id

inserted document inresult

I need to ask why? You should be able to do var id = Checklists.insert(Checklist)

on the client if the collection is published and let Meteor handle the sync with the server. Has it been sent to the Checklists

customer?

0


source







All Articles