【问题标题】:Scope not seeming to apply in MongoClient.connect范围似乎不适用于 MongoClient.connect
【发布时间】: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 进行操作。所以显而易见的事情是检查函数调用。

标签: node.js mongodb closures


【解决方案1】:

呃,愚蠢的问题,答案是因为 docNumber 被错误地解析为字符串而不是 int...

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 2023-04-06
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多