2017-04-23 181 views
1

目前,我有一個mongo數據庫,用於跟蹤用戶提交的答案。當用戶輸入他們已經做過的答案時,這是或不是,我想檢查是否有答案已經改變。問題是這隻有一半的時間。Javascript比較布爾值

下面我有一個叫做服務的帆服務,當用戶向Node.js服務器發送一個答案時,它從問題控制器調用。

QuestionServer.js

var ObjectID = require('mongodb').ObjectID; 

module.exports = { 

submitAnswer: function(tokenKey, questionId, answer, done) { 

    User.findOne({tokenKey: tokenKey}).exec(function (err, findOneRecord){ 
     if(findOneRecord) { 
      var qId = ObjectID(questionId); 
      QuestionsAnswered.findOrCreate({question: qId, user: findOneRecord.id}).exec(function createFindCB(error, createdOrFoundRecord){ 
       if(error) { 
        done(error, findOneRecord, null); 
       }else if(createdOrFoundRecord){ 
        var oldAnswerChanged = QuestionService.submitAnswerCheck(createdOrFoundRecord.answer, answer); 
        console.log("Old Changed Answer = " + oldAnswerChanged); 

        createdOrFoundRecord.answer = answer; 
        createdOrFoundRecord.save(function(saveErr) { 
         QuestionService.submitCount(questionId, answer, oldAnswerChanged, done); 
        }); 
       } 
      }); 
     } 
    }); 
}, 

submitAnswerCheck: function(recordAnswer, answer) { 
    console.log("recordAnswer = " + recordAnswer); 
    console.log("answer = " + answer); 

    recordAnswer = Boolean(recordAnswer); 
    answer = Boolean(answer); 

    if(recordAnswer != answer){ 
     return true; 
    } 

    return false; 
} 
} 

正如你可以從我的代碼見上面我submitAnswerCheck功能我想,以確保被傳遞給這個函數的布爾總是原語,從不布爾對象。

下面你會發現所有控制檯日誌輸出到服務器。

有關以下輸出的更多說明,更改!= to!==不會更改控制檯的輸出。下面的答案仍然是一樣的。

輸出到控制檯:

recordAnswer = true 
answer = true 
Old Changed Answer = false 

recordAnswer = true 
answer = false 
Old Changed Answer = false 

recordAnswer = false 
answer = false 
Old Changed Answer = true 

recordAnswer = false 
answer = true 
Old Changed Answer = true 

recordAnswer = true 
answer = true 
Old Changed Answer = false 

我想看到的是一個輸出示例2和4都爲「老更改答案」迴歸真實的,但似乎是發生的是,每當recordAnswer是false submitAnswerCheck始終返回true。

最後這裏是保存布爾到MongoDB的模型:

QuestionsAnswered.js

module.exports = { 

attributes: { 

question: { 
    model: "question", 
    required: true 
}, 
user: { 
    model: "user", 
    required: true 
}, 
answer: { 
    type: "boolean", 
    boolean: true 
} 
} 
}; 

我不知道如果我錯過理解布爾元和布爾對象,或者之間的區別在我的模型中設置布爾值時還有其他東西缺失。在比較javascript/sails.js中的布爾值時是否有其他人遇到過這個問題?

回答

0

發現問題

1日關閉,因爲這article介紹如何使用布爾函數是非常危險的,因爲它原來的JavaScript是很進取關於強制類型轉換。這意味着你不能通過布爾函數來轉換你的變量爲基元。

從這個我發現郵差送我的回答布爾值作爲字符串例如:

debug: answer = 'true' 

的解決方案是爲檢查,如果答案等於「真」一樣簡單,如下圖所示:

正確方法(QuestionController.js):

module.exports = { 

answer: function(req, res) { 

    var tokenKey = req.param("tokenKey"); 
    var questionId = req.param("questionId"); 
    var answer = (req.param("answer") == 'true'); 

    QuestionService.submitAnswer(tokenKey, questionId, answer, function(err, yes, no){ 
     return res.json({ 
      countYes: yes, 
      countNo: no, 
      err: err 
     }); 
    }); 
} 
}; 

不正確的方式:

module.exports = { 

answer: function(req, res) { 

    var tokenKey = req.param("tokenKey"); 
    var questionId = req.param("questionId"); 
    var answer = req.param("answer"); 

    QuestionService.submitAnswer(tokenKey, questionId, answer, function(err, yes, no){ 
     return res.json({ 
      countYes: yes, 
      countNo: no, 
      err: err 
     }); 
    }); 
} 
};