有没有办法让堆栈跟踪显示代码中调用保存的行?
我正在测试我的验证逻辑,并注意到 Mongoose 不会将堆栈跟踪打印到我调用的位置save()
。虽然验证确实说明了问题所在,但并没有说明问题所在。
const mySchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
accessToken: {
type: String,
required: true,
},
})
mySchema.statics.createOrUpdate = async function(name, accessToken) {
const animal = await this.findOne({ name })
if (!animal) {
animal = new Animal({ name }) // accessToken is missing and required
}
await animal.save() // expected stacktrace error here
}
Run Code Online (Sandbox Code Playgroud)
ValidationError: Animal validation failed: accessToken: Path `accessToken` is required.
at model.Document.invalidate (/Users/michael/repos/MyApp/node_modules/mongoose/lib/document.js:2622:32)
at /Users/michael/repos/MyApp/node_modules/mongoose/lib/document.js:2442:17
at /Users/michael/repos/MyApp/node_modules/mongoose/lib/schematype.js:1225:9
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Run Code Online (Sandbox Code Playgroud)
如果我重新抛出错误,我可以获得更具描述性的堆栈跟踪。但我宁愿不需要这样做:
...
await animal.save().catch((e) => { throw Error(e) })
Run Code Online (Sandbox Code Playgroud)
Error: ValidationError: accessToken: Path `accessToken` is required.
at /Users/michael/repos/MyApp/models/Animal.js:19:42
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
205 次 |
最近记录: |