2016-07-21 60 views
0

當試圖保存引發此錯誤的文檔:貓鼬.save() - 類型錯誤:_this [I] .emit不是一個函數

/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/utils.js:98 
process.nextTick(function() { throw err; }); 
          ^

TypeError: _this[i].emit is not a function 
at EventEmitter.notify (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/types/documentarray.js:238:18) 
at emitOne (events.js:82:20) 
at EventEmitter.emit (events.js:169:7) 
at Document.(anonymous function) [as emit] (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/document.js:93:44) 
at EventEmitter.<anonymous> (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/schema/embedded.js:31:15) 
at emitTwo (events.js:92:20) 
at EventEmitter.emit (events.js:172:7) 
at model.Document.(anonymous function) [as emit] (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/document.js:93:44) 
at /Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/model.js:227:11 
at /Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/lib/model.js:135:7 
at /Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/collection.js:504:5 
at /Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/collection.js:666:5 
at handleCallback (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12) 
at /Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:473:9 
at handleCallback (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12) 
at resultHandler (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:420:5) 
at commandCallback (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1246:9) 
at Callbacks.emit (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3) 
at null.messageHandler (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:397:23) 
at Socket.<anonymous> (/Users/home/Documents/web/thp/modules/thp_db/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:302:22) 
at emitOne (events.js:77:13) 
at Socket.emit (events.js:169:7) 
at readableAddChunk (_stream_readable.js:153:18) 
at Socket.Readable.push (_stream_readable.js:111:10) 
at TCP.onread (net.js:536:20) 

代碼導致保存看起來像這樣:

var db = req.app.locals.db; 
var league = new db.League(); 

// league is populated here 

// ================ 
// GIVE LEAGUE _id (auto increment) 
// ================ 
db.Counter.findByIdAndUpdate('League', 
          { $inc: { n: 1 } }, 
          { new: true }, 
    function(err, counter){ 
     if(err) return log.error('League _id set error', err); 

     league._id = counter.n; 

     // ================ 
     // INTO DB: CODE RUNS TO HERE 
     // ================ 

     league.save(function(err, new_league) { 

      // DOES NOT REACH HERE! 

      if (err) { 
       log.error('league DB entry error:', err); 
       return res.status(200).end('Error creating league'); 
      } 

      console.log('SAVED LEAGUE', util.inspect(new_league, false, null)); 

      res.status(200).end('Thank you'); 
     }); 
    } 
); 

奇怪的是,這工作得很好,昨晚,除非我夢遊並登錄,什麼也沒有改變。

db變量的模型的集合。

任何幫助將不勝感激。

+0

你怎麼定義'db.League'? – Manu

回答

1

原來,錯誤是根據哪個提交表單的輸入被填寫拋出。沒有時間來確定目前的確切時間,但是當我這樣做的時候會在這裏更新。

UPDATE:my var由從傳入表單數據解析的JSON對象填充。其中一個屬性被不經意地稱爲.on,這是愚蠢的標準事件聆聽記號.on。愚蠢的錯誤!永遠是。

相關問題