错误:回送已在回送中调用(Error: Callback was already called in loopback)

编程入门 行业动态 更新时间:2024-10-28 12:29:39
错误:回送已在回送中调用(Error: Callback was already called in loopback)

我有以下代码:

"use strict"; const Raven = require("raven"); Raven.config( "test" ).install(); module.exports = function(Reservation) { function dateValidator(err) { if (this.startDate >= this.endDate) { err(); } } function sendEmail(campground) { return new Promise((resolve, reject) => { Reservation.app.models.Email.send(formEmailObject(campground), function( err, mail ) { if (err) { console.log(err); Raven.captureException(err); reject(err); } else { console.log(mail); console.log("email sent!"); resolve(mail); } }); }); } function formEmailObject(campground) { return { to: "loopbackintern@yopmail.com", from: "noreply@optis.be", subject: "Thank you for your reservation at " + campground.name, html: "<p>We confirm your reservation for <strong>" + campground.name + "</strong></p>" }; } Reservation.validate("startDate", dateValidator, { message: "endDate should be after startDate" }); Reservation.observe("after save", async function(ctx, next) { try { const campground = await Reservation.app.models.Campground.findById( ctx.instance.campgroundId ); const mail = await sendEmail(campground); next(); } catch (e) { Raven.captureException(e); next(e); } }); };

抱歉格式不佳。 当流程完成时,我得到这个错误:

(node:3907) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called.

我在两个地方调用next()回调,一个在try代码中,另一个在catch代码中。 我认为当一切顺利时,下一次回调只会被调用一次,而在出错时也是如此。 但它似乎被称为两次,我不知道为什么。

我也尝试在try / catch代码之外调用next,但是它导致了相同的错误。 如果我只留下在catch代码中调用的下一个,它不会抛出错误。

任何想法? 谢谢!

I have the following code:

"use strict"; const Raven = require("raven"); Raven.config( "test" ).install(); module.exports = function(Reservation) { function dateValidator(err) { if (this.startDate >= this.endDate) { err(); } } function sendEmail(campground) { return new Promise((resolve, reject) => { Reservation.app.models.Email.send(formEmailObject(campground), function( err, mail ) { if (err) { console.log(err); Raven.captureException(err); reject(err); } else { console.log(mail); console.log("email sent!"); resolve(mail); } }); }); } function formEmailObject(campground) { return { to: "loopbackintern@yopmail.com", from: "noreply@optis.be", subject: "Thank you for your reservation at " + campground.name, html: "<p>We confirm your reservation for <strong>" + campground.name + "</strong></p>" }; } Reservation.validate("startDate", dateValidator, { message: "endDate should be after startDate" }); Reservation.observe("after save", async function(ctx, next) { try { const campground = await Reservation.app.models.Campground.findById( ctx.instance.campgroundId ); const mail = await sendEmail(campground); next(); } catch (e) { Raven.captureException(e); next(e); } }); };

Sorry for the poor formatting. When the flow is done I get this error:

(node:3907) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called.

I am calling the next() callback in two places, one in the try code and one in the catch code. I assume that when it all goes right, next callback is called only once, and the same when it goes wrong. But it seems that it is called twice and I don't know why.

I also tried to call next outside the try/catch code but it results in the same error. If I left only the next that is called inside the catch code it doesn't throw the error.

Any idea? Thanks!

最满意答案

如果你正在使用异步函数,你不应该明确调用next,它会自动调用。

看看这个github问题的回环异步/等待

所以你的钩子可以像下面这样。

Reservation.observe("after save", async ctx => { try { const campground = await Reservation.app.models.Campground.findById( ctx.instance.campgroundId ); const mail = await sendEmail(campground); } catch (e) { Raven.captureException(e); throw e; } });

注意:除非你想修改/处理错误,否则你不需要用try catch来包装它。

if you are using async function you shouldn't explicitly call next, it gets automatically called.

check out this github issue for loopback async/await

so your hook can be like the following.

Reservation.observe("after save", async ctx => { try { const campground = await Reservation.app.models.Campground.findById( ctx.instance.campgroundId ); const mail = await sendEmail(campground); } catch (e) { Raven.captureException(e); throw e; } });

NB: you don't need to wrap it in try catch unless you want to modify/work with the error.

更多推荐

本文发布于:2023-08-06 15:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回送   已在   错误   Error   called

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!