我正在开发一个 Node.js + Express。我的数据库是 Mongo,我正在使用 Mongoose 连接到这个数据库。
我试图以这种方式在 Mongoose 查询回调中引发异常:
game.save(function (err) {
if (err) {
throw new app.exception.EntitySaveFailed();
}
});
但是当我这样做时,节点服务器崩溃并显示此堆栈跟踪:
[object Object]
at Promise.<anonymous> (/home/server/routes/api/game.js:219:17)
at Promise.<anonymous> (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
at Promise.resolve (/home/server/node_modules/mongoose/lib/promise.js:108:15)
at Promise.<anonymous> (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
at Promise.fulfill (/home/server/node_modules/mongoose/node_modules/mpromise/lib/promise.js:92:20)
Forever detected script exited with code: 8
Forever restarting script for 9389 time
Listening on port 3000...
当我在猫鼬回调函数之外抛出异常时,一切正常。