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

I have this code on the client:var Checklist = {title: this.title,belongs_to: this.belongs_to,type: thi

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744980925a4604435.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信