gpt4 book ai didi

javascript - Feathersjs:如何发送响应并随后触发函数?

转载 作者:行者123 更新时间:2023-11-28 04:03:05 24 4
gpt4 key购买 nike

我有一些代码在 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。

我现在有一个用于消息服务的创建方法的 AFTER 钩子(Hook):

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”?

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

最佳答案

答案在 FAQ的羽毛!将响应发送给用户后如何进行处理:

这取决于你返回钩子(Hook)的 promise 。下面是一个发送电子邮件但不等待成功消息的 Hook 示例。

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);
}

关于javascript - Feathersjs:如何发送响应并随后触发函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46930444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com