Node.js 无法在查询中使用变量进行查找

Node.js 无法在查询中使用变量进行查找,node.js,mongodb,mongodb-query,Node.js,Mongodb,Mongodb Query,我当时正在NodeJS上的windows上做一个项目 我使用以下代码检查登录凭据。在我改成ubuntu 16.04之前,它一直运行良好 我设置了我的环境,现在变量没有被当作字符串处理 它正在从数据库返回空集。如果我这样做用户名:“某个用户名”,密码:“某个密码”,工作正常 请帮忙。我被卡住了:-(抱歉,如果这是一个noob类型的错误:-) 数据库结构: { "_id" : ObjectId("xxxxxxxxxxxxxxxxxxx"), "user_name" : "xxxxxx

我当时正在NodeJS上的windows上做一个项目

我使用以下代码检查登录凭据。在我改成ubuntu 16.04之前,它一直运行良好

我设置了我的环境,现在变量没有被当作字符串处理

它正在从数据库返回空集。如果我这样做
用户名:“某个用户名”,密码:“某个密码”
,工作正常

请帮忙。我被卡住了:-(抱歉,如果这是一个noob类型的错误:-)

数据库结构:

{
    "_id" : ObjectId("xxxxxxxxxxxxxxxxxxx"),
    "user_name" : "xxxxxxx",
    "pass" : "xxxxx",
    "type" : "root",
    "email" : "xxxx@gmail.com",
    "comp_name" : "NULL",
    "default_page" : "main",
    "name" : "xxxxx",
    "comp_id" : "NULL"
}

请按以下方法操作:-

app.post('/login', function(req, res, next) {
var N1 = (req.body.N1).toString();//Do toString()
var N2 = (req.body.N2).toString();
console.log(N1+N2);//Getting passed successfully
MongoClient.connect(url, function(err, db) {
    if (err) {
        throw err;
        console.log("into connectivity error");
    }
    console.log(N1);
    db.collection('col.login').find({user_name:N1,pass:N2}).toArray(function(err, result) {
        if (err) {
            throw err;
        }
        resultArray=result;
        try{
            req.session.userId = result[0]._id;
            req.session.userType = result[0].type;
            req.session.userComp = result[0].comp_id;
            req.session.cat = -1;
            if(result[0].type=="root") {
                res.redirect('/New_Company');
            }
      });

有关
toString()
的更多信息,请参阅
console.log(N1)的值?我检查了N1和N2,它们都是我在登录页面上发布的内容。你能分享你的数据库结构吗?当然,请稍等片刻。我不确定它为什么不工作。你能试着对N2和N1进行
toString()
,看看它是否有效吗?谢谢你的回答。
app.post('/login', function(req, res, next) {
var N1 = (req.body.N1).toString();//Do toString()
var N2 = (req.body.N2).toString();
console.log(N1+N2);//Getting passed successfully
MongoClient.connect(url, function(err, db) {
    if (err) {
        throw err;
        console.log("into connectivity error");
    }
    console.log(N1);
    db.collection('col.login').find({user_name:N1,pass:N2}).toArray(function(err, result) {
        if (err) {
            throw err;
        }
        resultArray=result;
        try{
            req.session.userId = result[0]._id;
            req.session.userType = result[0].type;
            req.session.userComp = result[0].comp_id;
            req.session.cat = -1;
            if(result[0].type=="root") {
                res.redirect('/New_Company');
            }
      });