node.js - Mongodb插入空日期

标签 node.js mongodb mongoose

邮件架构:

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/

相关文章:

javascript - Node.js 检测两个 Mongoose 查找何时完成

javascript - 关于帖子、评论、保存和喜欢的 Mongodb 数据结构

node.js - Mongoose 预保存绑定(bind)/此关键字不起作用

javascript - 为什么我的 Loop 事件处理程序不工作?

javascript - C++ 级别的 nodeJS 模块加载

node.js - 在for循环中更新MongoDB数据库中的每个元素

node.js - 在 Mongoose ODM 中查找嵌入文档

node.js - 有效的 geoNear 聚合查询在 Mongoskin 中不返回任何内容

javascript - Mongoose 保存一个空对象

mongodb - 如何通过一次调用将一组对象插入 Mongoose 数组中?