2014-06-28 41 views
0

我通過所有的集合試圖循環中貓鼬底層連接:我如何遍歷集合中的貓鼬

var chai = require('chai'), 
    expect = chai.expect, 
    mongoose = require('mongoose'), 
    db = mongoose.connection; 

describe('Blog Tests', function() { 

    before(function() { 
     console.log('before called'); 
     db.on('error', console.error); 

     mongoose.connect('mongodb://localhost/mydb', function(err){ 
      if(err)console.err(err); 

      // never loops through collections at this point! 
      for (var i in mongoose.connection.collections) { 
       console.log(mongoose.connection.collections[i]); 
       // will drop collection here 
      } 
     }); 
    }); 

    after(function(){ 
     console.log('after called'); 
     if(db.db._state === 'connected'){ 
      console.log('closing db'); 
      db.close(); 
     } 
    }); 

    it('Can add a blog post', function (done) { 
     db.on('open', function(){ 
      var blogPostSchema = new mongoose.Schema({ 
        id: mongoose.Schema.ObjectId, 
        title: {type: String, required: true}, 
        content: {type: String}, 
        tags: [] 
       }), 
       BlogPost = mongoose.model('BlogPost', blogPostSchema), 
       blogPost = new BlogPost({ 
        title: 'Hello World', 
        content: 'Lorem baby', 
        tags: [{"tag": "tests"},{"tag": "mocha"}, {"tag": "chai"}] 
       }); 

      blogPost.save(function(err, saved){ 
       if (err) console.error(err); 
       expect(saved.title).is.equal('Hello World'); 
       done(); 
      }); 
     }); 
    }); 
}); 

但它永遠不會返回集合 - 他們DEFS存在,它只是返回空物體;不是一個空陣列。

任何人都可以發現這個愚蠢的錯誤嗎?

更新爲每Gergo建議,但仍然沒有顯示的集合:

var chai = require('chai'), 
    expect = chai.expect, 
    mongoose = require('mongoose'), 
    db = mongoose.connection; 

describe('Blog Tests', function() { 

    before(function (done) { 
     console.log('before called'); 
     db.on('error', console.error); 

     mongoose.connect('mongodb://localhost/mydb', function(err){ 
      if(err) return console.err(err); 

      // never loops through collections at this point! 
      for (var i in mongoose.connection.collections) { 
       console.log(mongoose.connection.collections[i]); 
       // will drop collection here 
      } 
      done(); 
     }); 
    }); 

    after(function(done){ 
     console.log('after called'); 
     if(db.db._state === 'connected'){ 
      console.log('closing db'); 
      db.close(); 
      done(); 
     } 
    }); 

    it('Can add a blog post', function (done) { 
      var blogPostSchema = new mongoose.Schema({ 
        id: mongoose.Schema.ObjectId, 
        title: {type: String, required: true}, 
        content: {type: String}, 
        tags: [] 
       }), 
       BlogPost = mongoose.model('BlogPost', blogPostSchema), 
       blogPost = new BlogPost({ 
        title: 'Hello World', 
        content: 'Lorem baby', 
        tags: [{"tag": "tests"},{"tag": "mocha"}, {"tag": "chai"}] 
       }); 

      blogPost.save(function(err, saved){ 
       if (err) console.error(err); 
       expect(saved.title).is.equal('Hello World'); 
       done(); 
      }); 
    }); 
}); 

回答

0

mongoose.connect是異步的,的的mongoose.connect回調前塊結束被調用之前執行。使用done參數,並調用它的回調中:

before(function(done) { 
    console.log('before called'); 
    db.on('error', console.error); 

    mongoose.connect('mongodb://localhost/mydb', function(err){ 
     if(err) return console.err(err); 

     // never loops through collections at this point! 
     for (var i in mongoose.connection.collections) { 
      console.log(mongoose.connection.collections[i]); 
      // will drop collection here 
     } 
     done(); 
    }); 
}); 

此外,改變if(err) console.err(err);if(err) return console.err(err);,因爲你當前的代碼繼續,即便有一個錯誤的執行。使用if(err) return done(err);會更好。

編輯︰我測試了代碼,它確實無法正常工作。但是發現問題不在於異步執行,而是與mongoose.connection.collections。當連接打開時,mongoose.connection.collections的值是一個空對象{}。這是您的for循環無法按預期工作的原因。但是以下工作:

mongoose.connection.db.collectionNames(function(err, names) { 
    console.log(names); 
}); 

所以,你可以重寫你的代碼是這樣的:

before(function(done) { 
    console.log('before called'); 
    db.on('error', console.error); 

    mongoose.connect('mongodb://localhost/mydb', function(err) { 
     if (err) return done(err); 

     mongoose.connection.db.collectionNames(function(err, names) { 
      console.log(names); 
     }); 

     done(); 
    }); 
}); 
+0

感謝您的回覆Gergo,可惜mongoose.connection的回調函數內的mongoose.connection.collections仍然沒有按」 t獲取收藏並且連接已打開。我修改了上面的原始代碼。 - Chev 38分鐘ag – Chev

+0

@Chev更新了我的答案。 –

+0

似乎mongoose.connection.collections就像是貓鼬藏品的緩存 – Chev