Node.js 节点应用程序mongodb查询中的find()非常混乱
我试图获取mongodb集合中存储的文档中的字段值。我的代码不起作用,我不知道为什么。我知道find()返回一个cursor对象,我想使用cursor.next()遍历它。根据我的调试,代码中的那个点似乎就是它失败的地方。我正在使用node.js的mongodb模块来执行这些操作。代码如下: 设置数据库/集合信息:Node.js 节点应用程序mongodb查询中的find()非常混乱,node.js,mongodb,Node.js,Mongodb,我试图获取mongodb集合中存储的文档中的字段值。我的代码不起作用,我不知道为什么。我知道find()返回一个cursor对象,我想使用cursor.next()遍历它。根据我的调试,代码中的那个点似乎就是它失败的地方。我正在使用node.js的mongodb模块来执行这些操作。代码如下: 设置数据库/集合信息: exports.login = function(request, response){ var Db = require('mongodb').Db, Se
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;
打开数据库并进行查询:
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();
});
}
我似乎没有收到任何错误,页面只是无限期地“挂起”。我想什么也找不到。在mongo shell中测试查询,看看是否得到了什么。另外,请确保不要以纯文本形式存储密码!查看node bcrypt包。控制台日志?确保您访问了代码的某些部分?我进入else子句,然后它挂起<代码>集合。每个(函数(err,result){都是问题所在。在集合中循环如何?它是否为每个集合运行一个?它可能找不到任何与查询匹配的内容。控制台记录集合?[12:00:14 PM]db.profiles.find().limit(100)我不认为我得到了什么,我不确定我的查询出了什么问题。数据在记录中,应该与查询匹配。你能在mongo shell中获得它吗?当我在mongo shell中进行搜索时,会填充条件。当我通过节点进行查询时,它会显示空的find()条件,如上所述。mongo shell:db.profiles.find({“用户名”:“Jim.James”,“密码”:“pass”}).limit(50);我真的鼓励你去看看mongoose。它让这件事简单得多。