Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose说验证需要错误,甚至提供了值_Node.js_Mongoose - Fatal编程技术网

Node.js Mongoose说验证需要错误,甚至提供了值

Node.js Mongoose说验证需要错误,甚至提供了值,node.js,mongoose,Node.js,Mongoose,我在这个链接上有一个JSON数据文件 我的猫鼬模型是这样的 const { model, Schema } = require("mongoose"); const CountrySchema = new Schema({ name: { type: String, required: [true, "Bạn phải nhập tên quốc gia"], unique: true, }, code: {

我在这个链接上有一个JSON数据文件

我的猫鼬模型是这样的

const { model, Schema } = require("mongoose");

const CountrySchema = new Schema({
    name: {
        type: String,
        required: [true, "Bạn phải nhập tên quốc gia"],
        unique: true,
    },
    code: {
        type: String,
        required: [true, "Bạn phải nhập code"],
        unique: true,
    },
    capital: {
        type: String,
        required: true,
    },
    region: {
        type: String,
        required: true,
        ref: "Region",
    },
    currency: new Schema({
        code: {
            type: String,
            required: true,
        },
        name: {
            type: String,
            required: true,
        },
        symbol: {
            type: String,
            required: true,
        },
    }),
    language: new Schema({
        code: {
            type: String,
            required: true,
        },
        name: {
            type: String,
            required: true,
        },
    }),
    flag: {
        type: String,
        required: true,
    },
});

const Country = model("Country", CountrySchema);

module.exports = Country;

路线呢

const { Router } = require("express");
const fs = require("fs");
const Country = require("../models/Country");

const router = Router();


router.get("/country", async (req, res) => {
    try {
        let countries = await fs.readFileSync(
            __dirname + "/../install/countries.json",
            "utf-8"
        );
        countries = JSON.parse(countries);
        // countries = await Country.insertMany(countries);
        return res.json({
            success: true,
            message: "Init countries successful",
            data: {
                countries,
                length: countries.length,
            },
        });
    } catch (error) {
        res.json({
            success: false,
            message: "Init countries failed",
            error,
        });
    }
});

module.exports = router;

在运行向数据库插入数据的代码时,我收到了以下消息

{
    "success": false,
    "message": "Init countries failed",
    "error": {
        "errors": {
            "capital": {
                "message": "Path `capital` is required.",
                "name": "ValidatorError",
                "properties": {
                    "message": "Path `capital` is required.",
                    "type": "required",
                    "path": "capital",
                    "value": ""
                },
                "kind": "required",
                "path": "capital",
                "value": ""
            }
        },
        "_message": "Country validation failed",
        "message": "Country validation failed: capital: Path `capital` is required.",
        "name": "ValidationError"
    }
}

太令人困惑了。我不知道为什么?我仔细检查了每个元素上是否存在资本路径。那有什么问题吗?谁能给我解释一下这件事吗,非常感谢

未定义的空值违反了规定

在猫鼬中,它说:

验证程序不会在未定义的值上运行。唯一的例外是 必需的验证器

某些资本数据具有空的未定义值,如下所示:

  {
    "name": "Bouvet Island",
    "code": "BV",
    "capital": "",
    "region": "AN",
    "currency": {
      "code": "NOK",
      "name": "Norwegian krone",
      "symbol": "kr"
    },
    "language": {
      "code": "no",
      "name": "Norwegian"
    },
    "flag": "https://restcountries.eu/data/bvt.svg"
  },
只需删除capital field所需的选项,即可正常工作

或者,作为修复,在模式定义之前应用此代码,以便空值不会违反所需规则

mongoose.Schema.Types.String.checkRequiredv=>v!=无效的
我更新了答案,你能检查一下吗?也许它更适合你的应用程序。