2012-12-09 108 views
0

我與貓鼬瞎搞,而且我注意到,節約了無效項目時我find回調被多次調用:回調找到()被調用多次

persist.js

mongoose.model('ContentTag', new mongoose.Schema({ 
    content: requiredString, 
    tag:  requiredString 
})); 

test.js(使用摩卡)

describe('content tag', function() { 
    it('should not save an empty content tag', function(done) { 
     persist.ContentTag().save(function(err) { 
      assert.ok(err); 

      persist.ContentTag.find(function(err, items) { 
       assert.equal(0, items.length); 
       done(); 
      }); 
     }); 
    }); 

這將產生以下錯誤輸出:

Save err: ValidatorError: Validator "required" failed for path tag 
Save err: ValidatorError: Validator "required" failed for path content 
Find err: null 
Find err: null 

與以下錯誤:

1) Persist content tag should not save an empty content tag: 
    Error: done() called multiple times 
     at multiple (/usr/local/lib/node_modules/mocha/lib/runnable.js:168:31) 
     at done (/usr/local/lib/node_modules/mocha/lib/runnable.js:174:26) 
     at Runnable.run.duration (/usr/local/lib/node_modules/mocha/lib/runnable.js:190:9) 
     at persist.ContentTag.content (path/test/server-test.js:21:21) 
     at Query.execFind (path/node_modules/mongoose/lib/mongoose/query.js:572:26) 
     at Cursor.toArray (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:122:15) 
     at Cursor.each (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:167:11) 
     at Cursor.nextObject (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:467:28) 
     at Cursor.close (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:625:17) 
     at Cursor.nextObject (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:467:10) 
     at Cursor.nextObject.commandHandler (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:452:15) 
     at EventEmitter.emit (events.js:96:17) 
     at Db.open.self.serverConfig.connection.addListener.self.state (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/db.js:90:12) 
     at EventEmitter.emit (events.js:93:17) 
     at Connection.open.receiveListener (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/connection.js:82:16) 
     at Socket.Connection.open.receiveListener (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/connection.js:85:16) 
     at Socket.EventEmitter.emit (events.js:93:17) 
     at TCP.onread (net.js:391:31) 

看起來當有多個驗證錯誤一樣,保存回調爲每個錯誤調用一次。

當我刪除了new關鍵字,我得到以下錯誤:

1) Persist content tag should not save an empty content tag: 
    TypeError: Object #<Object> has no method 'buildDoc' 
     at Object.Document (../node_modules/mongoose/lib/mongoose/document.js:26:19) 
     at Object.Model (../node_modules/mongoose/lib/mongoose/model.js:24:12) 
     at Object.model [as ContentTag] 
+0

嘗試刪除'new'關鍵字? – theabraham

+0

有趣的想法,但它沒有奏效。 (OP已更新) –

+0

在回調中添加檢查和記錄「err」可能有助於診斷正在發生的事情。 – JohnnyHK

回答

1

回調save爲每個驗證err調用一次。因此,我多次致電done()。回想起來,沒有必要斷言err已被定義,並且find()調用是空的。