Node.js 猫鼬承诺如何跳到下一个。然后()

Node.js 猫鼬承诺如何跳到下一个。然后(),node.js,mongoose,promise,Node.js,Mongoose,Promise,我正在使用以下代码实现电子邮件验证 //1. find the verificationInfo VerificationInfo.findOne({user: req.user}).exec(function(err, verificationInfo) { if (err) return next(err) if (verificationInfo) { //2. if already have verification info

我正在使用以下代码实现电子邮件验证

//1. find the verificationInfo
VerificationInfo.findOne({user: req.user}).exec(function(err, verificationInfo) {
    if (err)
        return next(err)

    if (verificationInfo) {
        //2. if already have verification info
        var verificationLink = helper.urlWithQuery(req.baseUrl + '/verify', {token: verificationInfo.token})
        helper.sendEmail(email, 'verify your email', 'click the link: ' + verificationLink, function(err) {
            if (err)
                return next(helper.getGeneralError('unable to send email'))
            res.json({success: 'successfully sent email'})
        })
    }
    else {
        //3. no such verification info, create a new one
        var newVerificationInfo = new VerificationInfo()
        newVerificationInfo.user = req.user
        newVerificationInfo.token = helper.getUUID()
        newVerificationInfo.save(function(err) {
            if (err)
                return next(err)
            var verificationLink = helper.urlWithQuery(req.baseUrl + '/verify', {token: newVerificationInfo.token})
            helper.sendEmail(email, 'verify your email', 'click the link: ' + verificationLink, function(err) {
                if (err)
                    return next(helper.getGeneralError('unable to send email'))
                res.json({success: 'successfully sent email'})
            })
        })
    }
})
发送电子邮件部分重复。所以我把这个代码改成这样

VerificationInfo.findOne({user: req.user}).exec()
    .then(function(verificationInfo) {
        if (! verificationInfo) {
            var newVerificationInfo = new VerificationInfo()
            newVerificationInfo.user = req.user
            newVerificationInfo.token = helper.getUUID()
            req.token = newVerificationInfo.token
            return newVerificationInfo.save()
        }
        else {
            req.token = verificationInfo.token
            //TODO: HOW TO GET TO THE NEXT .then()
        }
    })
    .then(function() {
        var verificationLink = helper.urlWithQuery(req.baseUrl + '/verify', {token: req.token})
        helper.sendEmail(email, 'verify your email', 'click the link: ' + verificationLink, function(err) {
            if (err)
                return next(helper.getGeneralError('unable to send email'))
            res.json({success: 'successfully sent email'})
        })
    })
    .then(null, function(err) {
        return next(err)
    })

正如我在评论中指出的,如果verificationInfo已经存在,我如何才能转到下一个。然后()

这是其他地方的一个错误。我已决定取消存在检查。如果用户发送多个请求,我只创建一个新的请求,因为它很快就会过期,并且会被mongoose自动删除。这在其他地方是一个bug。我已决定取消存在检查。若用户发送多个请求,我只创建一个新的请求,因为它很快就会过期,并且会被mongoose自动删除

运行此代码时会发生什么?据我所知,它应该已经转到下一个
然后
(将发送电子邮件的那一个),没有任何更改。@VictorOHL
。然后()
不被调用somehow@OMGPOP:那一定是出了什么事。当前,您的承诺将解析为
未定义的
,然后将调用下一个
回调。要使代码更像原始版本,您可以尝试通过承诺链传递
令牌,而不是作为
req
的属性。为此,您需要一个内链
newVerificationInfo.save()。然后(…)
。如果出现错误,则您将发现
.save()
不可用,这可能是原始问题。@OMGPOP您确定它将转到else块吗?运行此代码时会发生什么情况?据我所知,它应该已经转到下一个
然后
(将发送电子邮件的那一个),没有任何更改。@VictorOHL
。然后()
不被调用somehow@OMGPOP:那一定是出了什么事。当前,您的承诺将解析为
未定义的
,然后将调用下一个
回调。要使代码更像原始版本,您可以尝试通过承诺链传递
令牌,而不是作为
req
的属性。为此,您需要一个内链
newVerificationInfo.save()。然后(…)
。如果出现错误,则您将发现
.save()
不可用,这可能是原始问题。@OMGPOP您确定它将转到else块吗?