MongoDB中插入空日期
邮件架构:
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}
}
]
});
当我尝试保存新邮件与结束日期为空,我有以下错误
Mail validation failed: endDate: Cast to Date failed for value "null"
回答如下:
它看起来像你要保存的模式与一个无效的结束日期。
如果你不希望保存的日期只是离开它,从对象。比如你需要为你的模式至少是:
const newMail = mail.create({
globalId: "1234",
from: "Jack",
begineDate: new Date(),
subject: "Random subject",
});
如果你试图做这样的事情,
endDate: null
因为它是期待一个Date()
对象或什么都没有,这将失败。