Я дважды запускал один и тот же код и не получал такой ошибки. Теперь я продолжаю получать:
(node:13368) UnhandledPromiseRejectionWarning: ValidationError: G2Alert validation failed: openDate: Path `openDate` is required.
Схема
const mongoose = require('mongoose');
const { Schema } = mongoose;
const G2AlertsSchema = new Schema(
{
status: { type: String, required: true },
openDate: { type: Date, required: true },
alertType: { type: Array, required: true },
severity: { type: Array, required: true },
locationName: { type: Array, required: true },
history: { type: Array, required: true },
alertDetails: { type: Array, required: false },
assignedTo: { type: Schema.Types.ObjectId, ref: 'user' },
},
{
timestamps: true,
},
);
const G2Alerts = mongoose.model('G2Alert', G2AlertsSchema);
module.exports = G2Alerts;
и вот код, который я пытаюсь запустить
const newAlertsData = [];
alertData.data.items.forEach(item => newAlertsData.push(item));
const locationName = [...new Set(newAlertsData.map(a => a.locationName))];
const alertType = [...new Set(newAlertsData.map(a => a.alertType))];
const severity = [...new Set(newAlertsData.map(a => a.severity))];
const openDate = [...new Set(newAlertsData.map(a => a.openDate))];
console.log(openDate, new Date(openDate[0]));
const newAlerts = new G2Alerts({
status: 'new',
openDate: new Date(openDate[0]),
alertType,
severity,
locationName,
history: [{ event: 'New alert added & assigned', date: new Date(), assignedTo: randomUser }],
alertDetails: newAlertsData,
assignedTo: user,
});
Эти две части:
const openDate = [...new Set(newAlertsData.map(a => a.openDate))];
console.log(openDate, new Date(openDate[0]));
верните это, и я пытаюсь добавить один из них. Я даже пробовал простую новую дату () и все равно не работает
[
'2020-07-12T17:58:40.390Z',
'2020-07-12T17:58:40.222Z',
'2020-07-12T17:58:39.967Z',
'2020-07-12T17:58:39.772Z',
'2019-05-16T13:00:48.330Z',
'2019-05-16T12:24:46.233Z',
'2019-05-16T09:52:23.574Z',
'2019-05-10T14:15:47.801Z'
]
2020-07-12T17:58:40.390Z