node.js mongodb - collection.find()。toArray(callback) - 回调不会被调用 [英] node.js mongodb - collection.find().toArray(callback) - callback doesn't get called

查看:2318
本文介绍了node.js mongodb - collection.find()。toArray(callback) - 回调不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用mongodb,但是我试图在集合上使用.find()时遇到问题。

I am just starting out with mongodb, but I am running into a problem when trying to use .find() on a collection.

我创建了一个DataAccessObject打开一个特定的数据库,然后让它执行操作。以下是代码:

I've created a DataAccessObject which opens a specific databate and then lets your perform operations on it. Here is the code:

构造函数

var DataAccessObject = function(db_name, host, port){
    this.db = new Db(db_name, new Server(host, port, {auto_reconnect: true}, {}));
    this.db.open(function(){});
}

A getCollection 函数:

DataAccessObject.prototype.getCollection = function(collection_name, callback) {
    this.db.collection(collection_name, function(error, collection) {
        if(error) callback(error);
        else callback(null, collection);
  });
};

save 功能:

DataAccessObject.prototype.save = function(collection_name, data, callback){
    this.getCollection(collection_name, function(error, collection){
        if(error) callback(error);
        else{
            //in case it's just one article and not an array of articles
            if(typeof (data.length) === 'undefined'){
                data = [data];
            }

            //insert to collection
            collection.insert(data, function(){
                callback(null, data);
            });
        }
    });
}

这似乎有问题 - findAll函数

And what seems to be the problematic one - a findAll function:

DataAccessObject.prototype.findAll = function(collection_name, callback) {
    this.getCollection(collection_name, function(error, collection) {
      if(error) callback(error)
      else {
        collection.find().toArray(function(error, results){
            if(error) callback(error);
            else callback(null, results);
        });
      }
    });
};

每当我尝试dao。 findAll(错误,回调) 回调永远不会被调用。
我已经将问题缩小到代码的以下部分:

Whenever I try to dao.findAll(error, callback), the callback never gets called. I've narrowed the problem down to the following part of the code:

collection.find().toArray(function(error, result){
    //... whatever is in here never gets executed
});

我看过其他人怎么做。实际上,我非常关注本教程。没有人似乎有这个问题colelction.find()。toArray(),它不会出现在我的搜索。

I've looked at how other people do it. In fact, I'm following this tutorial very closely. No one else seems to have this problem with colelction.find().toArray(), and it doesn't come up in my searches.

谢谢,
Xaan。

Thanks, Xaan.

推荐答案

您不使用 open 回调,因此如果您正在尝试在创建 dao 后立即创建 findall 请求,那么它将不会准备好。

You are not using the open callback so if you are trying to make the findall request right after creating the dao then it won't be ready.

如果你的代码是这样,它将无法工作。

If your code is like this, it will not work.

var dao = new DataAccessObject("my_dbase", "localhost", 27017);

dao.findAll("my_collection",function() {console.log(arguments);});

我测试了它,它没有找到记录,它也没有错误。我认为它应该给一个错误。

I tested it and it doesn't find records, and it also gives no error. I think it should give an error.

但如果你改变它,以便给一个回调的构造函数,那么它应该工作。

But if you change it so that you give a callback to the constructor, then it should work.

var DataAccessObject = function(db_name, host, port, callback){
    this.db = new Db(db_name, new Server(host, port, {auto_reconnect: true}, {}));
    this.db.open(callback);
}

让你的代码这样。

var dao = new DataAccessObject("my_dbase", "localhost", 27017, function() {
    dao.findAll("my_collection",function() {console.log(arguments);});
});

这篇关于node.js mongodb - collection.find()。toArray(callback) - 回调不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆