0

Inserted Object

newSale {
  productCode: 'MRS-GT-EN',
  amount: 359.89,
  serial: 4143,
  orderNumber: 2241,
  bookingDates: [
    { date: '9-1-2023', time: 6 },
    { date: '10-1-2023', time: 6 },
    { date: '11-1-2023', time: 6 },
    { date: '12-1-2023', time: 6 },
    { date: '13-1-2023', time: 6 },
    { date: '14-1-2023', time: 6 },
    { date: '15-1-2023', time: 6 },
    { date: '16-1-2023', time: 6 }
  ],
  delivered: false
}

with this Schema:

const saleSchema = new Schema({
    productCode: { type: String, required: true },
    amount: { type: Number, required: true },
    serial: { type: Number, required: true },
    orderNumber: { type: Number, required: true},
    bookindDates: [{date: {type: String}, time:{type:Number}}],
    delivered: { type: Boolean, required: true }
}, { timestamps: true})

When I review the DB saves everything fine except for bookingDates, it leaves an empty array.

I tried this enter link description here unsuccessfully.

I doing something wrong but I don't know what, any idea?

Thanks

AlZ
  • 19
  • 7

2 Answers2

1

You need to set type and default properties for bookindDates,

bookindDates: {
  type: [{ date: String, time: Number }]
  default: undefined
}
turivishal
  • 34,368
  • 7
  • 36
  • 59
  • Not working, now it does not even create the empty array. – AlZ Jan 04 '23 at 12:27
  • I was wondering if the problem is that im sending the whole array, but that does not makes any sense – AlZ Jan 04 '23 at 12:44
  • It will not create an empty array, that you actually wanted, It should work, what is your insert document? and what is the mongoose npm version? – turivishal Jan 05 '23 at 04:43
  • Im sending the data to the Schema exactly how it is in the upper code of the post. Also it saves everything else but the array. If you need more details here is the code to call the DB: newSale(saleData){ return this.db.then( _ => this.model.create(saleData)).then(resp => { return resp }) } – AlZ Jan 05 '23 at 08:59
  • Mongoose is 6.6.0 – AlZ Jan 05 '23 at 09:47
0
bookingDates: {type:[{date: {type: String}, time:{type:Number}}], default: undefined},

This is how ended up working, but also the main problem was the property name in the Schema was misspelled bookindDates instead bookingDates.

AlZ
  • 19
  • 7