猫鼬的臭虫?我不断验证失败,日期为必填路径

时间:2020-07-13 11:13:08

标签: javascript node.js mongodb express mongoose

我两次运行完全相同的代码,但没有得到这样的错误。现在我不断得到:

(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]));

返回此,我正在尝试添加其中之一。我什至尝试使用普通的新Date()仍然失败

[
  '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

0 个答案:

没有答案