来自React组件的流星方法响应和错误回调总是不确定的



,在服务器端我有方法:

Meteor.methods({
    'group.new'({ name, desc }) {
        if (!Meteor.userId()) {
            throw new Meteor.Error('You need to be logged in to comment');
        }
        Groups.insert({
            name,
            desc,
            createdBy: Meteor.userId(),
            created: new Date(),
        });
    },
});

我是从前端上的一个反应组件来称呼的:

        Meteor.call(
            'group.new',
            {
                name: this.state.name,
                desc: this.state.desc,
            },
            function(err, res) {
                console.log(err);
                console.log(res);
            },
        );

为什么ERR和RES总是不确定?

您似乎不会从服务器返回客户端。这就是为什么当插入成功时,客户端中的resp对象仍然不确定。错误对象应该有效。也许只是您没有达到错误条件。

这将有效:

server.js

Meteor.methods({
    'group.new'({ name, desc }) {
        if (!Meteor.userId()) {
            throw new Meteor.Error('You need to be logged in to comment');
        }
        // if insert succeeds, it will return the _id of the doc.
        var result_id = Groups.insert({
            name,
            desc,
            createdBy: Meteor.userId(),
            created: new Date(),
        });
         return result_id; // send the id back to the client
    },
});

现在,如果您在客户端上console.log(res),则应该能够查看从服务器发送回的插入文档的_id。

最新更新