Javascript 使用mongodb函数中的字符串

Javascript 使用mongodb函数中的字符串,javascript,mongodb,Javascript,Mongodb,需要有关node js和mongodb的帮助吗 我怎样才能退还resp?因为现在它返回“未定义” MongoClient.connect和collection.find是异步功能。 要正确获取resp的值,可以如下所示设置getData函数asynchronous function getData(table, where, to_select, callback) { // We pass callback function var r = {}; MongoClient.c

需要有关node js和mongodb的帮助吗 我怎样才能退还resp?因为现在它返回“未定义”


MongoClient.connect
collection.find
异步
功能。 要正确获取
resp
的值,可以如下所示设置
getData
函数
asynchronous

function getData(table, where, to_select, callback) { // We pass callback function 
    var r = {};
    MongoClient.connect("mongodb://127.0.0.1:27017/db", function(err, db) {
        if (!err) {
            collection = db.collection(table);
            collection.find(where, to_select).toArray(function(err, resp) {
             // r.t = resp; no need to assign as you want resp only
             callback(err,resp); // return resp
            })
        }else
           callback(err);// return with error 
    });
    //return r.t; r.t will be undefined as r.t is not updated yet, so we can remove this
}


/*Now you can call as below
  getData(whatever,)whatever,whatever,function(err,resp){
      console.log(resp);// You will get result here
  })*/

MongoClient.connect
collection.find
异步
功能。 要正确获取
resp
的值,可以如下所示设置
getData
函数
asynchronous

function getData(table, where, to_select, callback) { // We pass callback function 
    var r = {};
    MongoClient.connect("mongodb://127.0.0.1:27017/db", function(err, db) {
        if (!err) {
            collection = db.collection(table);
            collection.find(where, to_select).toArray(function(err, resp) {
             // r.t = resp; no need to assign as you want resp only
             callback(err,resp); // return resp
            })
        }else
           callback(err);// return with error 
    });
    //return r.t; r.t will be undefined as r.t is not updated yet, so we can remove this
}


/*Now you can call as below
  getData(whatever,)whatever,whatever,function(err,resp){
      console.log(resp);// You will get result here
  })*/

您需要使用回调,因为DB查询是异步操作。所以在find实际返回结果之前会调用return。这是解决办法

函数runQuery(表,其中,要选择,回调){
MongoClient.connect(“mongodb://127.0.0.1:27017/db,函数(err,db){
if(err){返回回调(err);}
collection=db.collection(表);
collection.find(where,to_select).toArray(function(err,resp){
回调(err,resp);
});
}); 
}

并调用您需要的函数

runQuery(table, where, select, function(err, results){
   ///do something with results.
});

希望这有帮助。

您需要使用回调,因为DB查询是异步操作。所以在find实际返回结果之前会调用return。这是解决办法

函数runQuery(表,其中,要选择,回调){
MongoClient.connect(“mongodb://127.0.0.1:27017/db,函数(err,db){
if(err){返回回调(err);}
collection=db.collection(表);
collection.find(where,to_select).toArray(function(err,resp){
回调(err,resp);
});
}); 
}

并调用您需要的函数

runQuery(table, where, select, function(err, results){
   ///do something with results.
});

希望这能有所帮助。

获取“回拨不是一项功能”您确定按照我们告诉您的方式运行。。回调它只是回调函数的一个名称。如果您直接复制代码,所有这些都应该工作。看看runQuery的例子,我甚至简化了一点。让我们来看看。获取“回调不是一个函数”您确定您按照我们告诉您的方式运行了吗。。回调它只是回调函数的一个名称。如果您直接复制代码,所有这些都应该工作。看看runQuery的例子,我甚至简化了一点,让我们来看看。