简体   繁体   English

范围似乎不适用于MongoClient.connect

[英]Scope not seeming to apply in MongoClient.connect

I'm trying to pass a variable into a Mongo query like this: 我试图像这样将变量传递到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 );
            } );
        }
    });

So I'm not entirely sure what's going on, but the docNumber (and any other variables not directly in the connect functions enclosing scope) are undefined in the connect function. 因此,我不确定是怎么回事,但是在连接函数中未定义docNumber(以及其他直接在连接函数中的变量)。 Any workarounds or obvious places where this is going wrong? 有什么解决方法或明显的地方哪里出了问题? I need to retrieve a doc from the database using a user defined docNumber. 我需要使用用户定义的docNumber从数据库中检索一个doc。

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

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

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