【发布时间】:2014-03-06 22:43:35
【问题描述】:
我正在尝试将变量传递给 Mongo 查询,如下所示:
function retrieveDocNumber( docNumber ) {
MongoClient.connect( 'mongodb://' + this.mongoUrl + "/" + this.mongoDbName, function(error, db) {
var lessonCollection = db.collection( 'lessons' );
if ( error ) {
console.log( error );
} else {
console.log( docNumber ) // Is undefined...
lessonCollection.findOne( {number: docNumber}, function( error, doc ) {
console.log( error );
console.log( doc );
} );
}
});
所以我不完全确定发生了什么,但是 docNumber(以及不直接在连接函数封闭范围内的任何其他变量)在连接函数中是未定义的。任何变通方法或明显的地方出错了?我需要使用用户定义的 docNumber 从数据库中检索文档。
【问题讨论】:
-
在retrieveDocNumber 函数中的任何地方都没有对docNumber 进行操作。所以显而易见的事情是检查函数调用。