简体   繁体   English

流星方法回调未定义结果

[英]Meteor Method callback getting undefined for result

I haven't coded in Meteor in a while, but I have this Meteor method that creates a task and returns the ID and another that appends that task to a project: 我已经有一段时间没有用Meteor编码了,但是我有这个Meteor方法可以创建一个任务并返回ID,另一个方法可以将该任务附加到项目中:

Meteor.methods({
  createTask(task) {
    // TODO Add a check here to validate
    Tasks.insert(task, (err, id) => {
      if (err) {
        throw new Meteor.Error(err);
      }
      id = {id: id};
      console.log('Returning id: ', id);
      return id;
    });
  }
});

Meteor.methods({
  appendTaskToProject(projectId, taskId) {
    // TODO Add check here to validate
    const project = Projects.findOne({_id: projectId});
    if (project) {
      if (!project.tasks) {
        project.tasks = [];
      }
      project.tasks.push(taskId);
      Projects.update({_id: projectId}, project, (err, id) => {
        if (err) {
          throw new Meteor.Error(err);
        }
      });
    } else {
      throw new Error("Could not find project");
    }
  }
});

I am attempting to call it on the client like thus: 我试图像这样在客户端上调用它:

Meteor.call('createTask', task, (err, taskId) => {
  console.log('err: ', err);
  console.log('taskId: ', taskId);
  if (err) {
    this.setState({ error: err.message});
  } else {
    Meteor.call('appendTaskToProject', projectId, taskId, (err, result) => {
      if (err) {
        this.setState({ error: err.message});
      } else {
        this.setState({ newTaskDialogOpen: false })
      }
    });
  }
});

The problem I am having is that taskId is not set in the callback. 我遇到的问题是未在回调中设置taskId From the method-side I see the log message inthe server like: 从方法方面,我在服务器中看到日志消息,例如:

I20180110-07:30:46.211(-5)? Returning id:  { id: 'a3nS9GcRhuhhLiseb' }

And in the client: 在客户端中:

Returning id:  {id: "a3nS9GcRhuhhLiseb"}id:
Tasks.jsx:43 err:  undefined
Tasks.jsx:44 taskId:  undefined

So I know it's returning something, but the callback is just not getting it. 所以我知道它正在返回某些东西,但是回调函数却没有得到它。 I know I should probably change the createTask to just take the task AND the projectId to link it too, but I would like to try and figure out why it's not getting the results of the Meteor method into the callback on the client-side. 我知道我可能应该更改createTask,使其只接受任务和projectId进行链接,但我想尝试弄清楚为什么它没有将Meteor方法的结果带到客户端的回调中。

The Meteor API documentation on collection methods like insert says the following: 关于收集方法(insert的Meteor API文档说明如下:

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. 在服务器上,如果不提供回调,则插入块,直到数据库确认该写入为止;否则,如果出现问题,则引发异常。 If you do provide a callback, insert still returns the ID immediately. 如果您提供回调,insert仍将立即返回ID。 Once the insert completes (or fails), the callback is called with error and result arguments. 插入完成(或失败)后,将使用错误和结果参数调用回调。 In an error case, result is undefined. 在错误情况下,结果是不确定的。 If the insert is successful, error is undefined and result is the new document ID. 如果插入成功,则错误是未定义的,结果是新的文档ID。

Applying this information to your code would create the following: 将此信息应用于您的代码将创建以下内容:

Meteor.methods({
  createTask(task) {
    // TODO Add a check here to validate
    return Tasks.insert(task, (err, id) => {
      if (err) {
        throw new Meteor.Error(err);
      }
    });
  }
});

This would return the new generated ID immediately but will have the disadvantage, that the error is thrown afterwards. 这将立即返回新生成的ID,但具有缺点,即随后会引发错误。 Therefore you better be going the direct way and execute "sync-like": 因此,最好采用直接方式并执行“类似同步”:

Meteor.methods({
  createTask(task) {
    // TODO Add a check here to validate
    return Tasks.insert(task);
  }
});

The meteor method automatically wraps the code so that on a positive return your client will receive a null value for error and the _id value for result. 流星方法自动包装代码,以便在返回正数时,您的客户端将收到错误的null值和结果的_id值。 If an error occurs during the insert, that method will automatically return the error in the client callback as error and the reuslt will be null. 如果在插入过程中发生错误,则该方法将在客户端回调中自动将错误作为错误返回,并且reuslt为null。

If you are concered with the synchronous nature of the code read this part of the guide about methhods. 如果您对代码的同步特性有所了解,请阅读指南中有关方法的这一部分

Same should apply for your update method: 同样适用于您的更新方法:

Meteor.methods({
  appendTaskToProject(projectId, taskId) {
    // TODO Add check here to validate
    return Projects.update({_id: projectId}, {$push: {tasks: taskId});
  }
});

Note, that I summarized this method to a more mongo oriented approach. 请注意,我将该方法总结为一种面向mongo的方法。

You need to return the id outside of the insert callback. 您需要在插入回调之外return ID。

Meteor.methods({
  createTask(task) {
    // TODO Add a check here to validate
    var returnID;
    Tasks.insert(task, (err, id) => {
      if (err) {
        throw new Meteor.Error(err);
      }
      id = {id: id};
      returnID = id;
      console.log('Returning id: ', id);
      // return id; --not here
    });
   return returnID; //return it here.
  }
});

Possible explanations can be found here . 可能的解释可以在这里找到。

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

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