簡體   English   中英

在節點中驗證用戶時獲取奇怪的輸出

[英]Getting weird output when authenticating user in node

我創建了一個函數,該函數可以獲取用戶的email & password ,然后在數據庫中運行幾次檢查以查看詳細信息是否有效。 如果詳細信息有效,則用戶將被解析回API密鑰。

但是由於某些原因,某些函數先於其他函數被調用,而我在一個假定返回userObj的變量上undefined

在該函數中,我添加了控制台日志0、1、2、4和5,因此您可以看到調用該函數時得到的奇怪輸出。 我將在下面包含我的代碼和控制台輸出! 我正在為模型使用sequalize ,因為我已經測試了它們,所以我知道它們正在返回正確的數據。 我也在用promises

碼:

var auth = {

  login: function(req, res) {

    var email = req.body.email || '';
    var password = req.body.password || '';

    // If nothing parsed, return json obj
    if (email == '' || password == '') {
      console.log("1");
      res.status(401);
      res.json({
        "status": 401,
        "message": "Invalid credentials"
      });
      return;
    }

    // Fire a query to your DB and check if the credentials are valid
    var dbUserObj = auth.validateUser(email, password);
    console.log("0 _____");
    console.log(dbUserObj); // I am returning undefined??? Why the!


    if (!dbUserObj) { // If authentication fails, we send a 401 back
      res.status(401);
      res.json({
        "status": 401,
        "message": "Invalid credentials"
      });
      return;
    }

    console.log("1 ____");
    authToken.create({ userId: dbUserObj.userId, token: genToken(dbUserObj)}).then(function(results){
      res.json("token", results.token);
    }); 
    console.log("2 ____");

  },
  validateUser: function(email, password) {
    // console.log(email + " pass: " + password);
    console.log("3 ____");

    user.findAll({
      where: {
        email : email,
        password : password
      },
      raw: true
    }).then(function(results) {
        console.log("4 _____");
        console.log(results);
        return results;
        // results prints out with all the correct values!!!!!!!!!!
    }).catch(function (err) {
      return err;
      console.log(err);
    });

    console.log("5 _____");
  },

控制台輸出:

您可以看到我在dbUserObj對象上變得未定義,但是老實說我不知道​​為什么,因為在實際返回它們BEFORE我先打印出結果(如下所示)。

3 ____
5 _____
0 _____
undefined
POST /login/ 401 1.341 ms - 46
Executing (default): SELECT `userId`, `name`, `password`, `email`, `authKey`, `profilePic`, `coverPic`, `description`, `createdAt`, `updatedAt` FROM `user` AS `user` WHERE `user`.`email` = 'jameswain10@gmail.com' AND `user`.`password` = 'f6588fc86e19db0936d9b7e8fd3d6c6544af6cdcc3ce161da88adcdb4b952adb';
4 _____ RESULTS ARE BELOW  ----
[ { userId: 1,
    name: 'James',
    password: 'f6588fc86e19db0936d9b7e8fd3d6c6544af6cdcc3ce161da88adcdb4b952adb',
    email: 'james@gmail.com',
    authKey: '$$$Ddjjdafjjadsfjadsjfjadfsj2',
    profilePic: 'default.png',
    coverPic: 'default.png',
    description: 'This user doesn\'t have a description!',
    createdAt: Wed Dec 23 2015 12:48:35 GMT+1100 (AEDT),
    updatedAt: Wed Dec 23 2015 12:48:35 GMT+1100 (AEDT) } ]

如您在上面的console output中所看到的,一些日志應該在注銷之前和之后注銷? 有誰知道為什么會這樣!?

您在使用Async時遇到問題。

有驗證用戶還諾:

validateUser: function(email, password) {
    // console.log(email + " pass: " + password);
    console.log("3 ____");

    return user.findAll({
      where: {
        email : email,
        password : password
      },
      raw: true
    });

在您的登錄功能,您可以then該函數的結果。

login: function(req, res) {

    var email = req.body.email || '';
    var password = req.body.password || '';

    // If nothing parsed, return json obj
    if (email == '' || password == '') {
      console.log("1");
      res.status(401);
      res.json({
        "status": 401,
        "message": "Invalid credentials"
      });
      return;
    }

    // Fire a query to your DB and check if the credentials are valid
    auth.validateUser(email, password)
    .catch(function() { 
            //.. handle error
        }
    .then(function(results) {
        var dbUserObj = results;

        console.log("0 _____");
        console.log(dbUserObj); // I am returning undefined??? Why the!


        if (!dbUserObj) { // If authentication fails, we send a 401 back
        res.status(401);
        res.json({
            "status": 401,
            "message": "Invalid credentials"
        });
        return;
        }

        console.log("1 ____");
        authToken.create({ userId: dbUserObj.userId, token: genToken(dbUserObj)}).then(function(results){
        res.json("token", results.token);
        }); 
        console.log("2 ____");
      }
  },

獎勵答案:

您變得不確定,因為此行在這里:

then(function(results) {
        console.log("4 _____");
        console.log(results);
        return results;
        // results prints out with all the correct values!!!!!!!!!!

這將結果返回到“ then”函數,該函數創建了另一個用您的dbobject解決的promise,但隨后您沒有將其返回給top函數。 在不返回承諾或調用回調的函數中使用promise是一個很好的警告,您在做錯事。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM