[英]How to resolve UnhandledPromiseRejectionWarning: ValidationError?
I tested all solutions I found on Stackoverflow associated with mongoose error. 我测试了在Stackoverflow上发现的与猫鼬错误相关的所有解决方案。
Anyways, I am facing a problem with the below piece of code. 无论如何,我下面的代码面临问题。 I think the problem is in something else, but I can't find it.
我认为问题出在其他地方,但我找不到。
I have other files linked with this one. 我还有与此相关的其他文件。
.finally catch ( it resolves part of the error) .finally catch(它解决了部分错误)
const assert = require('assert');
const User = require('../src/users');
describe('Test de virtualType', () => {
it("Test que le virtualType retourne bien le nombre de livre de l'utilisateur", (done) => {
const user1 = new User({
userName: 'Robin',
shopslist: [{ shopName: 'Le seigneur des anneaux' },{articleName:'Les raisins de la colère'}]
});
user1.save()
.then(() => User.findOne({ userName: 'Robin' }))
.then((User) => {
assert(user1.countShops.length===2);
})
.finally(done);
});
});
Terminal indicates : 终端指示:
(node:19900) UnhandledPromiseRejectionWarning: ValidationError: user validation failed: shopslist.0.articleName: L'articleName est requis, shopslist.1.shopName: Le shopName est requis
at new ValidationError
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:19900) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)```
UnhandledPromiseRejectionWarning
- You should handle error by using resolve()
and reject()
when you get a promise via .save()
method for User
.Then you can use: UnhandledPromiseRejectionWarning
当通过User
.save()
方法获得承诺时,应该使用resolve()
和reject()
处理错误,然后可以使用:
const user1 = new User(...);
user1.save()
.then(done =>{ //Do stuff here })
.catch(error =>{ console.log(error); })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.