Mongoose Schema,数组中的嵌套对象在创建时不起作用

时间:2018-04-16 05:22:21

标签: node.js mongodb express mongoose

在Mongoose中创建新文档时,我很难创建对象数组。我收到的数据看起来像这样:

name: 'Greg'
payments: [{
  payment_value: 100,
  payment_value_formatted: '$100.00'
}]

我的架构,像这样:

const mongoose = require('mongoose')
mongoose.Promise = global.Promise

const paymentSchema = new mongoose.Schema({
  payment_value: {
    type: Number,
  },
  payment_value_formatted: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  },
  date_formatted: {
    type: String,
    default: moment(new Date(Date.now())).format('MMM Do YY')
  }
})

const invoiceSchema = new mongoose.Schema({
  name: String,
  payments: [ paymentSchema ],
},
{ 
  timestamps: { 
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
})

module.exports = mongooseConnection.model('Invoice', invoiceSchema)

然后在我的控制器中:

let invoice = new Invoice(req.body);
    invoice = await invoice.save()

但这似乎并不适合我。我一直收到这个错误:

"Value has unknown type. Value: {
  payment_value: 100,
  payment_value_formatted: '$100.00', 
  date: 2018-04-16T05:08:17.287Z,
  date_formatted: 'Apr 16th 18',
  _id: 5ad42fc13f9d3791c1dc681b 
}"

0 个答案:

没有答案