mongodb nodejs存储连接外的值

时间:2021-03-23 03:05:41

I need a way to store value outside mongoDB connect call:

我需要一种方法来存储mongoDB连接调用之外的值:

read(object) {
    let result
    MongoClient.connect(this.url, function (err, db) {
        if (err!=null){
            result = err;
        } else {
            db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                assert.equal(err, null);  
                db.close();       
                result = docs; 
            }); 
        }
    }); 
    return result
}

When i call this method, which is part of a class, return is called before result assignment, as normal. Example: console.log(read(obj)) returns undefined

当我调用此方法时,它是类的一部分,返回在结果分配之前被调用,正常情况下。示例:console.log(read(obj))返回undefined

The idea is to store value in a variable and return might wait until connect terminate.

我们的想法是将值存储在变量中,并且返回可能会等到连接终止。

Is there any way to resolve this problem?

有什么方法可以解决这个问题吗?

2 个解决方案

#1


0  

Without promise:

没有承诺:

Call return inside find and err function:

在find和err函数中调用return:

read(object) {
    let result
    MongoClient.connect(this.url, function (err, db) {
        if (err!=null){
            result = err;
            return result; //here
        } else {
            db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                assert.equal(err, null);  
                db.close();       
                result = docs;
                return result;  // here
            }); 
        }
    });
}

Or set a timeout for return with enough time to wait for other process to end:

或者设置返回超时,并有足够的时间等待其他进程结束:

read(object) {
    let result
    MongoClient.connect(this.url, function (err, db) {
        if (err!=null){
            result = err;
        } else {
            db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                assert.equal(err, null);  
                db.close();       
                result = docs; 
            }); 
        }
    }); 
    setTimeout(function(){ return result}, 3000);   // 3secs
}

#2


0  

With Promise you can try the following:

使用Promise,您可以尝试以下方法:

function read(object) {
    let result
    return new Promise((resolve, reject) => {
        MongoClient.connect(this.url, function (err, db) {
            if (err!=null){
                reject(err);
            } else {
                db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                    db.close();   
                    if (err) {
                        reject(err);
                    } else {
                        resolve(docs);                                        
                    }
                }); 
            }
        });
    });
}

// then you can call the function and use the result like this
read(obj).then(docs => {
    console.log(docs);
})
.catch(err => {
    // handle error
    console.log(err);
})

#1


0  

Without promise:

没有承诺:

Call return inside find and err function:

在find和err函数中调用return:

read(object) {
    let result
    MongoClient.connect(this.url, function (err, db) {
        if (err!=null){
            result = err;
            return result; //here
        } else {
            db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                assert.equal(err, null);  
                db.close();       
                result = docs;
                return result;  // here
            }); 
        }
    });
}

Or set a timeout for return with enough time to wait for other process to end:

或者设置返回超时,并有足够的时间等待其他进程结束:

read(object) {
    let result
    MongoClient.connect(this.url, function (err, db) {
        if (err!=null){
            result = err;
        } else {
            db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                assert.equal(err, null);  
                db.close();       
                result = docs; 
            }); 
        }
    }); 
    setTimeout(function(){ return result}, 3000);   // 3secs
}

#2


0  

With Promise you can try the following:

使用Promise,您可以尝试以下方法:

function read(object) {
    let result
    return new Promise((resolve, reject) => {
        MongoClient.connect(this.url, function (err, db) {
            if (err!=null){
                reject(err);
            } else {
                db.collection(object.collection).find(object.field).toArray(function(err, docs) {
                    db.close();   
                    if (err) {
                        reject(err);
                    } else {
                        resolve(docs);                                        
                    }
                }); 
            }
        });
    });
}

// then you can call the function and use the result like this
read(obj).then(docs => {
    console.log(docs);
})
.catch(err => {
    // handle error
    console.log(err);
})