Sunday, 23 April 2017

Sails.js Comparing Booleans

Currently, I have an mongo database that keeps track of answers users submit. When a user makes an input for an answer they already did, that is yes or no I want to check to see if there answer has changed. The issue is this only working half of the time.

Below I have a sails service called Questions Service which is called from the Questions Controller when a user send an answer to the node.js server.

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;
}
}

As you can see from my code above in my submitAnswerCheck function I want to make sure that the booleans being passed to this function are always primitives and never boolean objects.

Below you will find the output of all the console logs going to the server.

Additional note about the output below, changing != to !== does not change the out from the console. Answers below were still the same.

Output to console:

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

What I would like to see is example output 2 and 4 both return true for the "Old Changed Answer", but what seems to be happening is that whenever the recordAnswer is false the submitAnswerCheck is always returning true.

Lastly here is the model that is saving the boolean to mongodb:

QuestionsAnswered.js

module.exports = {

attributes: {

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

I am not sure if I am miss understanding the difference between boolean primitives and boolean objects or if there is something else I am missing when setting up a boolean in my model. Have other people been having this problem when comparing boolean in javascript/sails.js?



via Tangela Wiz

No comments:

Post a Comment