回调函数在我的帖子请求中不起作用

时间:2017-02-15 07:23:22

标签: javascript express callback

我有回调代码:

function getUserToken(data, callback) {
    var password_sha256 = sha256(data.password);
    getAppById(data.app_id).then(function(res) {
        console.log("app"+res);
            if (!res) {
                console.log("No app");
                callback(err, { meta: {
                    code: 403,
                    error_message: "There are no app with your id!"
                } });
            } else {
                if (res.user_password == password_sha256) {
                    console.log("user found");
                    callback(err, { meta: { code: 200 },
                                    token: password_sha256,
                                    type: 1 });
                    return;
                } else if (res.owner_password == password_sha256) {
                    console.log("owner found");
                    callback(err, { meta: { code: 200 },
                                    token: password_sha256,
                                    type: 0 });
                } else {
                    console.log("user not found");
                    callback(err, { meta: {
                                    code: 403,
                                    error_message: "There are no users with your password!"
                                } });
                }
            }
    });
}

我使用此功能发布了一些数据:

router.post('/api/login', (req, res) => {
    db.getUserToken(req.body, function(err, result) {
        console.log(result);
        if (result.error) {
            return res.status(403).send(result);
        } else {
            return res.status(200).send(result);
        }
    });
});

然后getUserToken找到了,例如,用户,所以我有"用户找到"在控制台日志中,但/ api / login中的回调函数无法正常工作。我的错误在哪里,我该如何解决?

2 个答案:

答案 0 :(得分:0)

您应该使用回调(null,data)来代替使​​用回调(错误,数据)。这将为您提供所需的输出。

答案 1 :(得分:0)

您可以返回Promise,而不是回调,无论是原生还是使用外部库,如Kris Kowal Q

var Q = require('q');

function getUserToken(data) {
    var deferred = Q.defer();

    var password_sha256 = sha256(data.password);
    getAppById(data.app_id).then(function(res) {
        if (!res) {
            deferred.reject("There are no app with your id!");
        } else {
            if (res.user_password == password_sha256) {
                deferred.resolve({
                    token: password_sha256,
                    type: 1
                });
            } else if (res.owner_password == password_sha256) {
                deferred.resolve({
                    token: password_sha256,
                    type: 0
                });
            } else {
                console.log("user not found");
                deferred.reject("There are no users with your password!");
            }
        }
    })
    .catch(function(error) {
        deferred.reject(error);
    });

    return deferred.promise;
}

然后在发布数据时简单地处理或捕获错误

router.post('/api/login', (req, res) => {
    db.getUserToken(req.body)
      .then(function(result) {
          res.status(200).send(result);
      })
      .catch(function(error) {
          res.status(403).send(error);
      });
});