mongoose find()返回空数组

时间:2016-12-01 08:07:07

标签: node.js mongodb mongoose

我从git克隆了easyERP源代码。他们有这些代码行来生成某种模式。这是最新发生的事情

  1. 连接正常

  2. 已成功生成mainDBSchema

  3. var main似乎也很好,当我控制这个对象时,我得到了模型和模型。

  4. 唯一的问题是main.find()。exec()返回空数组。我绑了各种各样的黑客,但似乎没什么用。

    var mainDb = mongoose.createConnection('localhost', 'mainDB');
    
    mainDb.on('error', console.error.bind(console, 'connection error:'));
    mainDb.once('open', function callback() {
    console.log("Connection to mainDB is success");
    var mainDBSchema = mongoose.Schema({
        _id: Number,
        url: { type: String, default: 'localhost' },
        DBname: { type: String, default: '' },
        pass: { type: String, default: '' },
        user: { type: String, default: '' }
    }, { collection: 'easyErpDBS' });
    
    var main = mainDb.model('easyErpDBS', mainDBSchema);
    
    main.find().exec(function (err, result) {
        if (!err) {
            result.forEach(function (_db, index) {
                var dbInfo = {
                    DBname: '',
                    url: ''
                };
                var dbObject = mongoose.createConnection(_db.url, _db.DBname);
                dbObject.on('error', console.error.bind(console, 'connection error:'));
                dbObject.once('open', function callback() {
                    console.log("Connection to " + _db.DBname + " is success" + index);
                    dbInfo.url = result[index].url;
                    dbInfo.DBname = result[index].DBname;
                    dbsArray[index] = dbObject;
                    dbsNames[index] = dbInfo;
                });
            });
        } else {
            console.log(err);
        }
    });
    });
    

0 个答案:

没有答案