如何在错误对象中打印 属性 值?

How can I print a property value in the error object?

我正在制作一个简单的用户注册网络应用程序。我正在使用 MongoDB 和 Express JS 来注册新用户并将其保存在数据库中。

我在模式中添加了一些验证属性,每当用户填写不符合模式的无效数据时,控制台将显示验证错误。

现在,我要做的是在控制台中写入的验证错误有两个我想要的属性,即“路径”和“种类”,因此我可以将这两个显示在供用户查看的浏览器。

如何从错误对象中获取“路径”和“种类”属性?我不想打印整个错误消息,我只想要这两个属性。

我尝试使用 console.log(err.path)console.log(err.kind),但它们都 return 未定义。

我不知道如何访问这两个属性。

// Post new user (REGISTER):
router.post('/signup', async (req, res) => {

    const username = req.body.username;
    const email = req.body.email;
    const password = req.body.password;

    try {
        await User.create({username, email, password})
    } 

    catch(err) {

// I don't want the whole err, I just want err.path and err.kind if possible!
        console.log(err);
    }

});

以下应该有效。这些属性位于嵌套对象中,由关键字 errors

持有
console.log(err.errors.username.kind, err.errors.username.path)