简体   繁体   English

节点应用程序mongodb查询中的find()非常令人困惑

[英]find() in node app mongodb query very confusing

Im trying to get the value of a field located in a document stored in a mongodb collection. 我试图获取位于mongodb集合中存储的文档中的字段的值。 My code is not working and I'm not sure why. 我的代码无法正常工作,我不确定为什么。 I know find() returns a cursor object, which I want to traverse using cursor.next(). 我知道find()返回一个游标对象,我想使用cursor.next()进行遍历。 It seems that point in the code is where it's failing based on my debugging. 根据我的调试,似乎代码中的要点是它失败了。 I'm using the mongodb module for node.js to perform these operations. 我将mongodb模块用于node.js来执行这些操作。 Here is the code: 这是代码:

Setting up the DB/collection info: 设置数据库/收集信息:

exports.login = function(request, response){
    var Db = require('mongodb').Db,
        Server = require('mongodb').Server,
        assert = require('assert'),
        server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}),
        udb = new Db('users', server_config, {
            w: -1
        }),
        uname = request.body.uName,
        pw =  request.body.uPass;

The opening the db and querying: 打开数据库并查询:

 udb.open(function(err, udb) {
        assert.equal(null, err);
        var collection = udb.collection('profiles');
        collection.find({username: uname, password: pw}, function(err, collection){
            if( err || !collection){
                console.log("login fail");
                response.redirect('/');
            }
            else {
                collection.each(function(err, result){
                    assert.equal(null, err);
                    console.log("login success");
                    request.session.loggedIn = true;
                    request.session.user = uname;
                    response.redirect('/userDashboard');
                });
            }
        });
        udb.close();
    });
}

I don't seem to be getting any errors, the page just 'hangs' indefinitely. 我似乎没有任何错误,页面只是无限期地“挂起”。

I think nothing's being found. 我认为什么也没找到。 Test the query in the mongo shell and see if you're getting anything. 在mongo shell中测试查询,看看是否有任何东西。 Also be sure not to store passwords in plain text! 另外请确保不要以纯文本形式存储密码! Check out the node bcrypt package. 签出节点bcrypt包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM