猫鼬{strict:throw}不会抛出错误 [英] Mongoose {strict: throw} doesn't throw error

查看:66
本文介绍了猫鼬{strict:throw}不会抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在所有地方找到该问题的答案,但似乎我很不走运.

I tried to find the answer to this question everywhere, but it seems I'm out of luck.

我有一个非常简单的猫鼬模型

I have a very simple mongoose model

var userObject = {
    profile: {
        username: {
            type: String,
            required: true,
            lowercase: true
        },
        firstname: {
            type: String,
            required: true
        },
        lastname: {
            type: String,
            required: true
        },
        img: {
            type: String,
            required: true,
            match: /^(https?:\/\/)/i
        },
        email: {
            type: String,
            match: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
            required: true
        },
        singupdate: {
            type: Date,
            default: Date.now
        }
    }
};

而且,当我创建架构时,我选择了在不添加模型中的属性时引发错误的选项.

And, when I create the schema I choose the option to throw an error when I add properties not in the model.

new mongoose.Schema(userObject, { strict: "throw" });

这是我尝试捕获错误的方法.当我添加有效属性时,该过程将运行,并且会接收创建的文档,但是当我添加无效属性时,该过程将永远不会退出,并且日志也永远不会出现在控制台上.

This is how I tried to catch the error. When I add valid properties the process runs and I recibe the docs created, but when I add invalid properties, the process never exits, and the logs never appear on the console.

try {
    User.create(users, function(err, docs) {
        console.log("err: " + err);
        console.log("docs: " + docs);
    });
} catch (e) {
    console.log(e.message);
}

我在做什么错了?

推荐答案

如果添加不属于模型的属性,请从

If you add properties that are not part of the model, from mongoose doc :

strict选项(默认情况下启用)可确保将值传递给 在我们的模式中未指定的模型构造函数不会得到 保存到数据库

The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db

即使使用strict:throw,它也可以像这样工作,因此您不必担心模型中未引用的其他字段.

It is working like this even with strict:throw, so you don't have to worry about additional fields not referenced in the model.

关于未触发的异常,Aaron Heckmann在

Concerning the exception not triggered, Aaron Heckmann talks about this in this post regarding that an exception that is not triggered on a save with strict : throw :

这更是对猫鼬如何工作的误解. 严格" 选项启用对试图进行验证的键/值的验证 存储在数据库中.模式在文档实例上创建获取器/设置器 落后于已验证的doc.{g,s}et()方法. 将 adhoc 数据附加到猫鼬文档实例不会触发 get/set(),因此由于没有 那天可以保存到数据库的方式.

This is more a misunderstanding of how mongoose works. the 'strict' option enables validation of keys/values that are attempting to be stored in the db. schemas create getters/setters on document instances that delagate to the doc.{g,s}et() methods which are validated. attaching adhoc data to a mongoose document instance doesn't trigger get/set() and as such doesn't warrant validation since there's no way that day can get saved to the db.

由于其他字段不是模型的一部分,因此它们不会触发那些验证,因此不会触发异常(当然这些字段不会保存在数据库中)

As the additional fields are not part of the model, they don't trigger those validation so no exception is triggered (and of course those fields are not saved in the database)

仅当您属于模型的字段未通过此验证时,才会引发异常

Exceptions will be thrown only if your fields that belong to the model fail this validation

这篇关于猫鼬{strict:throw}不会抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆