Mongoose验证错误,但我把文件正确 [英] Mongoose validation error but I put documents correctly

查看:86
本文介绍了Mongoose验证错误,但我把文件正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看我的代码。我有一个验证错误,但我确信我把我的文件格式正确。



我的模型

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

var orderSchema = new Schema({
userPurchased:{type:Schema.Types.ObjectId,ref:'users'},
products:[
{
product:{type:Schema.Types.ObjectId,ref:'products'},
size:{type:String,required:true},
quantity:{type:Number,required:
subTotal:{type:Number,required:true}
}
],
totalQuantity:{type:Number},
totalPrice:{type: Number},
otherShipAd:{type:String},
modeOfPayment:{type:String},
paidStatus:{type:Boolean,default:false}
});

module.exports = mongoose.model('orders',orderSchema);

我的路线



$ {code> ordersRouter.route('/ placeOrder')
.post(function(req,res){
var body = req.body;
console.log(req.body);
var orderItem = {
userPurchased:body.userId,
products:[{
product:body._id,
size :body.size,
数量:body.quantity,
subTotal:body.subTotal
}],
totalQuantity:body.totalQuantity,
totalPrice:body.totalPrice ,
otherShipAd:body.customAdd,
modeOfPayment:body.modeOfPayment
};
Orders.create(orderItem,function(err,result){
if(err )throw err;
});
});

我的来自POSTMAN的JSON对象

  {
userPurchased:5887f303c58a953360fe2759,
products:[{
product:58466e8e734d1d2b0ceeae00
size:m,
quantity:3,
subTotal:1197
},
{
product 58466e8e734d1d2b0ceeae00,
size:l,
quantity:3,
subTotal:1197
}],
totalQuantity 6,
totalPrice:2394,
otherShipAd:,
modeOfPayment:BDO
}

请查看我的错误堆栈跟踪



编辑:req.body的结果



我在这里做错了什么?我被卡住了。

解决方案

在创建一个新的订单。看看内容 req.body ,看看你传递给 / code> object。



尝试这样:

  var orderItem = new Orders(); 
orderItem.userPurchased = body.userId;
//对于每个产品项目,将其推送到orderItem.products
body.products.forEach(function(product,index)
{
orderItem.products.push({
product:product.product,
size:product.size,
quantity:product.quantity,
subTotal:product.subTotal
});
} );
orderItem.totalQuantity = body.totalQuantity;
orderItem.totalPrice = body.totalPrice;
orderItem.otherShipAd = body.customAdd;
orderItem.modeOfPayment = body.modeOfPayment;

Orders.save(function(err,result){
if(err)throw err;
});


Please take a look at my code. I am having a validation error but I am quite sure that I put my documents on the correct format.

MY MODEL

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

var orderSchema = new Schema({
    userPurchased: { type: Schema.Types.ObjectId, ref: 'users' },
    products: [
        {
            product: { type: Schema.Types.ObjectId, ref: 'products' },
            size: { type: String, required: true },
            quantity: { type: Number, required: true },
            subTotal: { type: Number, required: true }
        }
    ],
    totalQuantity: { type: Number },
    totalPrice: { type: Number },
    otherShipAd: { type: String },
    modeOfPayment: { type: String },
    paidStatus: {type: Boolean, default: false}
});

module.exports = mongoose.model('orders', orderSchema);

MY ROUTE

ordersRouter.route('/placeOrder')
        .post(function (req, res) {
            var body = req.body;
            console.log(req.body);
            var orderItem = {
                userPurchased: body.userId,
                products: [{
                    product: body._id,
                    size: body.size,
                    quantity: body.quantity,
                    subTotal: body.subTotal
                }],
                totalQuantity: body.totalQuantity,
                totalPrice: body.totalPrice,
                otherShipAd: body.customAdd,
                modeOfPayment: body.modeOfPayment
            };
            Orders.create(orderItem, function (err, result) {
                if (err) throw err;
            });
        });

MY JSON OBJECT FROM POSTMAN

{
    "userPurchased": "5887f303c58a953360fe2759",
    "products": [{
        "product": "58466e8e734d1d2b0ceeae00",
        "size": "m",
        "quantity": 3,
        "subTotal": 1197
    },
    {
        "product": "58466e8e734d1d2b0ceeae00",
        "size": "l",
        "quantity": 3,
        "subTotal": 1197
    }],
    "totalQuantity": 6,
    "totalPrice": 2394,
    "otherShipAd": "",
    "modeOfPayment": "BDO"
}

Please see my error stack trace

EDIT: Result of req.body

What am I doing wrong in here? I'm stuck.

解决方案

You are doing mistake while creating a neworder. Look at the contents of req.body and look what you are passing to the orders object.

Try this:

var orderItem = new Orders();
orderItem.userPurchased=body.userId;
//for each products item, push it to orderItem.products
body.products.forEach(function(product,index)
{
    orderItem.products.push({
        product: product.product,
        size: product.size,
        quantity: product.quantity,
        subTotal: product.subTotal
    }); 
});
orderItem.totalQuantity=body.totalQuantity;
orderItem.totalPrice=body.totalPrice;
orderItem.otherShipAd=body.customAdd;
orderItem.modeOfPayment=body.modeOfPayment;

Orders.save(function (err, result) {
    if (err) throw err;
});

这篇关于Mongoose验证错误,但我把文件正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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