简体   繁体   中英

Meteor.call Callback Function Returns 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 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;
                }
            }
        );
    },

The Meteor.call passes the information successfully to the server, as the checklist is created. I can see in the server console the ID of the new checklist. However, the client only sees undefined for both error and result.

You don't return result in your server method. You can't return values from callback. Return just 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 Meteor docs , insert method returns ID of inserted document.

On the server, if you don't provide a callback, then insert blocks until the database acknowledges the write, or throws an exception if something went wrong.

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

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

The meteor.call callback know how to handle the server responde thats why you are using error result , if something gets wrong on the method the server will throw an error and the meteor call will fail.

Simplified to the bare minimum:

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

Your client code should see the _id of the inserted document in result

I do have to ask though, 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 synchronization to the server. Is Checklists not published to the client?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM