【发布时间】:2018-04-24 23:12:32
【问题描述】:
我升级了我的猫鼬,所以我当然开始得到这些:
DeprecationWarning: Mongoose: mpromise (mongoose's default promise library)
is deprecated, plug in your own promise library instead:
http://mongoosejs.com/docs/promises.html
所以我添加了mongoose.Promise = global.Promise。都好。除了......现在我得到了这个人:
(node:20760) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: exception: Index with name: stampRoundedToNearestHour_1 already exists with different options
(node:20760) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
修复潜在错误很容易,但我不知道应该在哪里捕获它,这样以后的 Node 版本就不会突然出现如果他们遇到类似的事情会崩溃。我已经生成了产生错误的代码的最小版本。这是一个 javascript 文件:
var mongoose = require('mongoose')
mongoose.Promise = global.Promise
var TestSchema = mongoose.Schema({
num: Number,
})
TestSchema.index({'num': 1}, { sparse: true })
TestSchema.index({'num': 1}, { sparse: false })
// The above line is deliberately designed to be problematic.
// My question isn't how to not get an error,
// it's that I don't know where to catch it when I do!
// If this line is commented out, there's no error
// but it doesn't return a promise, so I can't .then or .catch on it
var Test = mongoose.model('Test', TestSchema)
mongoose.connect(process.env.MONGOLAB_URI, {useMongoClient: true})
.then(function () {
console.log("mongoose.connected successfully")
}).catch(function (err) {
console.log("mongoose.connect catch", err)
})
如您所见,我在 mongoose.connect() 函数上尝试了两种错误处理,但运行时输出的是
mongoose.connected successfully
(node:26795) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: exception: Index with name: num_1 already exists with different options
(node:26795) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我已尝试将.catch(...) 添加到此处的所有其他函数中:
mongoose.Schema(...)TestSchema.index({'num': 1}, { sparse: true })mongoose.model('Test', TestSchema)- 甚至
require('mongoose')(这感觉很愚蠢,但我试过了)
...以及我系统中的一些其他功能,我可以在保持代码损坏的同时删除这些功能。
但在所有这些情况下TypeError: WHATEVER.catch is not a function。
我应该在哪里看到这个MongoError? (再一次,我知道如何防止它)
【问题讨论】:
-
使用
.then(successFunction, errorFunction)有点忽略了.catch(),这对于.then(null,errorFunction)来说真的只是“糖”。这是产生错误的所有代码吗?您确实“不应该”在实际连接“之后”建立模型,但这并不是真正的错误原因。所以这实际上只是我在这里看到的承诺链语法。除非可能有其他代码? -
简单地说。试试看:
mongoose.connect(process.env.MONGOLAB_URI, {useMongoClient: true}).then( () => console.log('connected') ).catch( err => console.error(err ) );,因为你的现有代码中似乎有未声明的变量应该抛出“未捕获的异常” -
@NeilLunn 是的,我知道。我很茫然,因为这实际上是所有代码,所以我把
.catch()卡在TypeError允许我的任何地方。这实际上是一个 js 文件,如果你运行它就会产生这个错误。 -
“只是尝试”的东西完全可以正常工作。不确定您对未声明变量的含义。在现有代码中,模型是在建立连接之前建立的,我只是尝试将
mongoose.connect(...)部分放在底部,它仍然错误。 -
那是不同的。索引定义是错误的。但这“是一个人为的案例”,除非您在这种情况下特别需要捕获该特定错误,否则您可以很容易地避免一开始就不这样做。事实上,猫鼬文档建议您“关闭自动索引”并使用“特定代码”来创建任何索引。此外,这不是您最初发布的问题。
标签: node.js mongodb mongoose promise mongoose-schema