简体   繁体   English

如何解决UnhandledPromiseRejectionWarning:ValidationError?

[英]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.

相关问题 UnhandledPromiseRejectionWarning:ValidationError - UnhandledPromiseRejectionWarning: ValidationError 如何在 node.js 服务器中使用 Jest 解决 UnhandledPromiseRejectionWarning - How do I resolve a UnhandledPromiseRejectionWarning with Jest in node.js server 在尝试部署 node.js 应用程序时,如何解决“UnhandledPromiseRejectionWarning”错误? - How would one resolve a 'UnhandledPromiseRejectionWarning' error, while trying to deploy a node.js app? 如何解决 MongoDb UnhandledPromiseRejectionWarning: E​​rror: queryTxt ETIMEOUT cluster0.3zwxl.mongodb.net? - How to resolve MongoDb UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0.3zwxl.mongodb.net? 无法解决我的猫鼬查询中的UnhandledPromiseRejectionWarning问题 - Can not resolve UnhandledPromiseRejectionWarning issue in my mongoose query 如何避免节点中的 UnhandledPromiseRejectionWarning - How to avoid UnhandledPromiseRejectionWarning in Node 如何解决节点上的 UnhandledPromiseRejectionWarning? - how to solve UnhandledPromiseRejectionWarning on node? 如何处理`UnhandledPromiseRejectionWarning` - How to handle `UnhandledPromiseRejectionWarning` UnhandledPromiseRejectionWarning,如何解决? - UnhandledPromiseRejectionWarning, how to fix it? NodeJS - 如何处理 UnhandledPromiseRejectionWarning? - NodeJS - How to deal with UnhandledPromiseRejectionWarning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM