从.then()函数返回值

时间:2016-08-03 11:15:00

标签: javascript promise

我需要将属性failedLoginsNum分配给其中一个错误属性,但是我遇到了从storeFailedLogins(req)函数返回此值的麻烦。这是功能:

function storeFailedLogin(req) {
    var failedLoginsNum; //want to return this value
    req.config.database.find('FailedLogins', {ip:req.connection.remoteAddress})
    .then(function (results) {
        if (!results.length) {
            req.config.database.create('FailedLogins', {
                timestamp: Date.now(),
                failedLoginsNum: 1
            });
            failedLoginsNum = 1;
        } else {
            req.config.database.update('FailedLogins', {ip: req.connection.remoteAddress}, {
                timestamp: Date.now(),
                failedLoginsNum: results[0].failedLoginsNum + 1
            });
            failedLoginsNum++;
        }
    });
    return failedLoginsNum; //trying to return it like that now, but getting undefined
}

这就是我需要使用该属性的方法:

var failedLoginsNum = storeFailedLogin(req);
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, {
    message: 'Invalid email address or password.',
    failedLogins: failedLoginsNum
});

0 个答案:

没有答案