使用Mongoose将数组插入Mongo无法正常工作

时间:2018-04-29 14:12:53

标签: mongodb mongoose

我正在尝试创建一个Mongo文档,但是插入到该文档的orderProducts元素的数组中是行不通的。

这是创建订单并插入Mongo的方法:

PptApp.Run Ppt1.Name & "!Module1.UpdateOLELinks"

使用此段代码解析数据时:

exports.createOrder = function(req, res) {

    var delivery = JSON.parse(req.body.delivery);
    var payment = JSON.parse(req.body.payment);
    var orderProducts = JSON.parse(req.body.orderItems);

    console.log('delivery', delivery)
    console.log('payment', payment)
    console.log('orderProducts', orderProducts);

    var newOrder = new Order({delivery, payment, orderProducts});
   // newOrder.orderProducts.push(orderProducts);

    console.log('new order', newOrder)

    res.json({'a':'b'})
}

我得到了预期的结果:

var delivery = JSON.parse(req.body.delivery);
var payment = JSON.parse(req.body.payment);
var orderProducts = JSON.parse(req.body.orderItems);

但是当我使用这段代码创建Mongo文档时:

delivery:
 { firstName: 'fasdfasdf',
   lastName: 'dfasdas',
   email: 'asdfdfs@a',
   phone: 'addafdafd',
   shippingAddress: 'fasdfasdf',
   cityState: 'Brighton, MA',
   deliveryDate: 2018-04-06T00:00:00.000Z }

 payment:
  { paymentType: 'PayPal',
    cardNumber: 'fdasasdfdfaf',
    creditCardMonth: '11 - November',
    creditCardYear: '2028',
    cws: 'afd' },

orderProducts [ { productId: '596161e1734d1d25634366ce', quantity: 1 },
  { productId: '596269eb734d1d256343976a', quantity: 1 },
  { productId: '5962a556734d1d256343a210', quantity: 1 } ]

newOrder的交付和付款元素如上所示是正确的,但对于orderProducts,数据丢失。我只得到以下内容:

var newOrder = new Order({delivery, payment, orderProducts});

这是订单的Mongoose模型:

orderProducts:
 [ { _id: 5ae5cb11dba7821f1840475f },
   { _id: 5ae5cb11dba7821f1840475e },
   { _id: 5ae5cb11dba7821f1840475d } ]

如何将数组插入文档?

0 个答案:

没有答案