我应该在哪里捕获这个 Promise MongoError?
Where am I supposed to catch this Promise MongoError?
我升级了我的猫鼬,所以我当然开始得到这些:
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()
函数上尝试了两种错误处理,但是当我 运行 this is
时输出的是什么
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
? (再一次,我知道如何预防)
你得到这个的原因是定义了重复的索引。
您应该只有其中一行。
TestSchema.index({'num': 1}, { sparse: true })
TestSchema.index({'num': 1}, { sparse: false })
Unhandled promise
实际上来自 node-mongodb-native 中与 createIndex
相关的拒绝。这不会暴露在 mongoose 中供您捕获错误。
可能的逻辑推理很简单,您的模式定义中永远不应该有错误。这是could/should在开发过程中很容易发现的,以后不会再出现了。
仔细查看后发现 this article,这个问题的一个可能答案是添加以下行:
process.on('unhandledRejection', function(error) {
// do something with this error
})
这样做意味着警告消失:
(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.
但我实际上并不知道这会阻止 Node.js 进程退出。
我升级了我的猫鼬,所以我当然开始得到这些:
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()
函数上尝试了两种错误处理,但是当我 运行 this is
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
? (再一次,我知道如何预防)
你得到这个的原因是定义了重复的索引。
您应该只有其中一行。
TestSchema.index({'num': 1}, { sparse: true })
TestSchema.index({'num': 1}, { sparse: false })
Unhandled promise
实际上来自 node-mongodb-native 中与 createIndex
相关的拒绝。这不会暴露在 mongoose 中供您捕获错误。
可能的逻辑推理很简单,您的模式定义中永远不应该有错误。这是could/should在开发过程中很容易发现的,以后不会再出现了。
仔细查看后发现 this article,这个问题的一个可能答案是添加以下行:
process.on('unhandledRejection', function(error) {
// do something with this error
})
这样做意味着警告消失:
(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.
但我实际上并不知道这会阻止 Node.js 进程退出。