邮件架构:
var mail = new mongoose.Schema({
globalId: {type: String, index: true, unique: true},
from: {type: String, required: true},
beginDate: {type: Date, required: true},
endDate: {type: Date},
source: {type: String},
address: {type: String},
subject: {type: String, required: true},
text: {type: String},
note: {type: String},
files: [
{
fileName: {type: String},
fileSize: {type: Number},
fileType: {type: String}
}
]
});
当我尝试保存 endDate 为 null 的新邮件时出现以下错误
Mail validation failed: endDate: Cast to Date failed for value "null"
最佳答案
看起来您正在尝试使用无效的结束日期来保存架构。
如果您不想保存日期,只需将其从对象中删除即可。例如,您至少需要为架构提供的是:
const newMail = mail.create({
globalId: "1234",
from: "Jack",
beginDate: new Date(),
subject: "Random subject",
});
如果你尝试做类似的事情,
结束日期:null
这将失败,因为它需要一个 Date()
对象或什么都没有。
关于node.js - Mongodb插入空日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52923615/