1

我有一些代码适用于 REST 端点,其中消息是:

  1. 在数据库中创建
  2. 步骤A已处理
  3. 当stepA正常时,返回响应消息
  4. 步骤B已处理。

这是代码:

  // POST single message
  app.post('/message', (req, res) => {
    const url = req.body.properties.url
    const image = req.body.properties.image
    const extraField = req.body.properties.extraField
    db.message.create({
      url: url,
      image: image,
    })
      .then(() => myProcess(extraField, 'stepA'))
      .then(newMessage => res.json(newMessage))
      .then(() => myProcess(extraField, 'stepB'))
  })

现在我正在尝试使用 feathersjs,但我不知道如何准确地执行 2、3、4。

我现在有一个用于消息服务的 create 方法的 AFTER 钩子:

module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
  return function processNewMessage (hook) {

    const { extraField } = hook.data.properties
    Promise.resolve(myProcess(extraField, 'stepA'))
      .then( <<NO-IDEA>> ) // Send RESPONSE!!
      .then(() => myProcess(extraField, 'stepB'))

    return Promise.resolve(hook);
  };
};

所以我的问题归结为:如何发送响应并随后使用 feathersjs 触发“myProcess stepB”?

尽管是“遗产”,但我认为它可能仍然相关。

4

1 回答 1

0

在feathersjs的FAQ中有回答!向用户发送响应后如何处理:

这取决于您在钩子中返回的承诺。这是一个发送电子邮件但不等待成功消息的钩子示例。

function (hook) {

  // Send an email by calling to the email service.
  hook.app.service('emails').create({
    to: 'user@email.com',
    body: 'You are so great!'
  });

  // Send a message to some logging service.
  hook.app.service('logging').create(hook.data);

  // Return a resolved promise to immediately move to the next hook
  // and not wait for the two previous promises to resolve.
  return Promise.resolve(hook);
}
于 2017-10-25T14:08:13.050 回答