永远不会分配Javascript变量

时间:2014-01-10 08:47:30

标签: javascript node.js

我有以下javascript但由于某种原因我的var响应始终设置为默认值false,即使结果为true

var response = false;

connection.query('SELECT * FROM Something WHERE something = ?', [req.body.something], function(err, results)
{
    if (results.length == 0)
    {
        response == false;
    }
    else
    {
        hashedPassword = results[0].password;

        var comparePassword = encrypt.comparePassword(req.body.user.password, hashedPassword, function(err,result)
            {
                if (err) {throw error;}
                response == result;
                console.log("res=" + response);
                console.log('here ' + result);
                res.send(result);
            }
        );

    }

    console.log("response " + response);
});

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

这是错误的 - >

response == result;

应该是:

response = result;

double ==用于测试条件,单个=用于分配。

答案 1 :(得分:0)

 response == result

不会分配给response。你确定这是你想要的吗?

答案 2 :(得分:0)

将您的代码更改为并尝试::

var response = false;

connection.query('SELECT * FROM Something WHERE something = ?', [req.body.something],  function(err, results)
   {
if (results.length == 0)
{
    response == false;
}
else
{
    hashedPassword = results[0].password;

    var comparePassword = encrypt.comparePassword(req.body.user.password, hashedPassword, function(err,result)
        {
            if (err) {throw error;}
            response = result;
            console.log("res=" + response);
            console.log('here ' + result);
            res.send(result);
        }
    );

}

console.log("response " + response);
})

在循环中设置一些条件时使用此特定运算符==,或者一般情况下,它不会更改变量的值,=用于赋值,这将更改变量