gpt4 book ai didi

node.js - Mongoose .save() -- TypeError : _this[i]. 发出不是一个函数

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:44 26 4
gpt4 key购买 nike

尝试保存文档时会抛出此错误:

/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 变量是模型的集合。

任何帮助将不胜感激。

最佳答案

事实证明,错误的引发取决于填写提交表单的哪些输入。目前没有时间确定具体是哪一个,但我会在此处更新。

更新:我的 league var 由从传入表单数据解析的 JSON 对象填充。其中一个属性被轻率地命名为 .on,这是对标准事件监听符号 .on 的愚弄。愚蠢的错误!始终如此。

关于node.js - Mongoose .save() -- TypeError : _this[i]. 发出不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38507964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com