简体   繁体   English

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

[英]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. 在创建核对表时,Meteor.call会将信息成功传递给服务器。 I can see in the server console the ID of the new checklist. 我可以在服务器控制台中看到新核对表的ID。 However, the client only sees undefined for both error and result. 但是,客户端只会看到错误和结果的undefined

You don't return result in your server method. 您不在服务器方法中返回结果。 You can't return values from callback. 您无法从回调中返回值。 Return just result of Checklists.insert: 返回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. 根据Meteor docsinsert方法返回插入文件的ID。

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. meteor.call callback知道如何处理服务器响应,这就是你使用error result ,如果方法出错,服务器将抛出错误并且流星调用将失败。

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 您的客户端代码应该在result看到插入文档的_id

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. 如果集合已发布,您应该能够在客户端上执行var id = Checklists.insert(Checklist) ,并让Meteor处理与服务器的同步。 Is Checklists not published to the client? Checklists是否未发布给客户?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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