在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
}"