我试图在我的mongodb中注册用户,如果进展顺利,我想给注册用户发送一封电子邮件。为此,我使用sendgrid,它附带了一个.send(msg),它返回承诺.catch(错误)和.then({.})。
如果出现错误,我想发送一个回复到网页,用户已注册,但电子邮件失败。我不能直接从.catch()执行此操作,因为它位于应用程序的另一部分,超出了范围,应该由多个其他函数使用.
sendgrid.js
module.exports.sendRegMail = function (user, password, adminUserID, adminName, success, errorCB) {
    msg.to = email;
    msg.subject = `Welcome ${user.name}! You have been registered!`;
    msg.text = `Dear ${user.name}.\nYou just been registered as a ${user.userType}.\n\nPlease log in with following credentials:\nUsername: ${user.username}\nPassword: ${password}\n\nOBS: it is important that you change your password to a personal one in order to use the platform!\nMost features will be disabled until your account has been secured with a personal password.`;
    sendMail(msg, success, errorCB);
}
function sendMail(msg, success, errorCB){
    sgMail.send(msg)
    .then(() => {
        Logger.logUserAction(userID, name, 'Successfully sent email:\n' + JSON.stringify(msg));
        success();
    })
    .catch(error => {
        Logger.logUserAction(userID, name, 'Tried to send email:\n' + JSON.stringify(msg) + '\nBut failed due to error:\n' + error);
        errorCB();
    });
}从用户被保存的地方删除..。
 User.addUser(newUser, (err, user) => {
                if (err) {
                    Logger.logAdminAction(decoded.data._id, decoded.data.name, 'Tried to register user: ' + user + '. but failed. Error: ' + err);
                    res.json({ success: false, msg: 'Failed to register user' });
                } else {
                    Logger.logAdminAction(decoded.data._id, decoded.data.name, 'Successfully created user: ' + user);
                    mail.sendRegMail(user, req.body.password, decoded.data._id, decoded.data.name, ()=>{
                        res.json({ success: true, msg: 'User registered, And email sent successfully!' })
                    }, () =>{
                        res.json({ success: true, msg: 'User registered, but email could not be sent! Contact the person manually.' })
                    });  
                }
            });现在,我试图在sendRegMail()中给出两个回调函数作为参数。然后在.then()中调用一个回调,在.catch()中调用另一个回调,但在我看来,这种方式过于复杂了吗?从父函数中处理承诺错误/成功的正确方法是什么?
发布于 2018-01-11 14:35:08
它不起作用,因为您从不将称为您的回调。你只需要提到它们(基本上是不操作的)。要打电话给他们,你会让()跟着他们:
function sendMail(msg, success, errorCB){
    sgMail.send(msg)
    .then(() => {
        Logger.logUserAction(userID, name, 'Successfully sent email:\n' + JSON.stringify(msg));
        success();
// ------------^^
    })
    .catch(error => {
        Logger.logUserAction(userID, name, 'Tried to send email:\n' + JSON.stringify(msg) + '\nBut failed due to error:\n' + error);
        errorCB();
// ------------^^
    });
}说,为什么不回承诺呢?
function sendMail(msg) {
    return sgMail.send(msg)
    .then(() => {
        Logger.logUserAction(userID, name, 'Successfully sent email:\n' + JSON.stringify(msg));
        // Nothing particularly useful to return here, so just leave it;
        // the promise will resolve with `undefined`
    })
    .catch(error => {
        Logger.logUserAction(userID, name, 'Tried to send email:\n' + JSON.stringify(msg) + '\nBut failed due to error:\n' + error);
        throw error; // <== Because we haven't *handled* it and want it to propagate
    });
}然后你就可以像这样使用它:
sendMail("message")
.then(() => { /* it worked */ })
.catch(err => { /* it failed */ });...and将其构建到链中,并在async函数中使用await和所有其他可以通过简单回调提供的有用功能。
发布于 2018-01-11 14:34:07
你错过了“成功()”和"errorCB()“的括号
https://stackoverflow.com/questions/48209455
复制相似问题