猫鼬必需的验证器失败值未定义

时间:2019-08-07 22:31:44

标签: node.js mongodb mongoose

我尝试将数据保存在猫鼬模式中,但是所需的验证程序失败,错误消息说该值未定义,但之前的日志指出它具有一个值。

我尝试使用新的mongoose.Schema而不是新的Schema,但这不能正常工作。

当我删除所需标签时,它保存了,但是它是空的: {bookingNumber:{_id:5d4b47d7685936130080eb70,__v:0}}

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BookingNumberSchema = new Schema({
    number: {
        type: Number,
        required: true,
    },
    description: {
        type: String,
        required: true,
    },
});

const BookingNumber = mongoose.model('BookingNumber', BookingNumberSchema);

module.exports = BookingNumber;



function createBookingNumber(req, res){ 

    console.log(req.body);
    BookingNumber.create(req.body)
        .then(bookingNumber =>{
            let reply = {
                "bookingNumber": bookingNumber
            }
            res.status(200);
            console.log(reply);
            res.send(reply);
        })
        .catch(err =>{
            res.status(400);
            console.log(err);
            res.send(err);
        });
}

在我的app.js中,我使用bodyParser.json并过滤nosql攻击:

app.use(bodyParser.json());

app.use(filter());

邮递员数据:

{
    "bookingNumber": { 
        "number": 1,
        "description": "example bookingnumber"
    }
} 

回复:

{
    "errors": {
        "description": {
            "message": "Path `description` is required.",
            "name": "ValidatorError",
            "properties": {
                "message": "Path `description` is required.",
                "type": "required",
                "path": "description"
            },
            "kind": "required",
            "path": "description"
        },
        "number": {
            "message": "Path `number` is required.",
            "name": "ValidatorError",
            "properties": {
                "message": "Path `number` is required.",
                "type": "required",
                "path": "number"
            },
            "kind": "required",
            "path": "number"
        }
    },
    "_message": "BookingNumber validation failed",
    "message": "BookingNumber validation failed: description: Path `description` is required., number: Path `number` is required.",
    "name": "ValidationError"
}

console.log:

{ bookingNumber: { number: 1, description: 'example bookingnumber' } }
{ ValidationError: BookingNumber validation failed: description: Path `description` is required., number: Path `number` is required.
        message: 'Path `description` is required.',
        name: 'ValidatorError',
        properties: [Object],
        kind: 'required',
        path: 'description',
        value: undefined,
        reason: undefined,
        [Symbol(mongoose:validatorError)]: true },
     number:

我看起来输入值没有传递给猫鼬,但是我找不到原因。

1 个答案:

答案 0 :(得分:1)

在您发送数据的邮递员中:

{
   "bookingNumber": { 
       "number": 1,
       "description": "example bookingnumber"
   }
} 

和您的模式BookingNumber一样,没有像您发送带有嵌套对象的“ bookingNumber”那样的键,猫鼬验证器没有检查“ number”和“ description”,这就是您的请求失败的原因。  只是像这样发送:

{ 
   "number": 1,
   "description": "example bookingnumber"
}