我有以下代码:
"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.
更多推荐
发布评论