Meteor.call回调函数返回未定义

Tyl*_*ter 3 javascript mongodb node.js meteor

我在客户端上有这个代码:

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);
                // }
            }
        );
Run Code Online (Sandbox Code Playgroud)

这在服务器上:

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;
                }
            }
        );
    },
Run Code Online (Sandbox Code Playgroud)

在创建核对表时,Meteor.call会将信息成功传递给服务器.我可以在服务器控制台中看到新核对表的ID.但是,客户端只能看到undefined错误和结果.

Tom*_*nik 5

您不在服务器方法中返回结果.您无法从回调中返回值.返回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;
                }
            }
        );
    },
Run Code Online (Sandbox Code Playgroud)

根据Meteor docs,insert方法返回插入文件的ID.

在服务器上,如果您不提供回调,则插入块直到数据库确认写入,或者如果出现错误则抛出异常.