最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor.call Callback Function Returns Undefined - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Apr 24, 2015 at 7:50 Getz 4,0636 gold badges37 silver badges52 bronze badges asked Apr 24, 2015 at 3:24 Tyler ShusterTyler Shuster 4351 gold badge5 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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?

发布评论

评论列表(0)

  1. 暂无评论