【问题标题】:How do i set array of string as a field in mongoose schema?如何将字符串数组设置为猫鼬模式中的字段?
【发布时间】:2020-10-16 05:46:31
【问题描述】:

我正在尝试使用以下字段在 mongoose 中创建会议模型-

    const meetingSchema = new mongoose.Schema({
    meeting_id: {
        type: String,
        required: true
    },
    title: {
        type: String,
        required: true
    },
    date: {
        type: String,
        required: true
    },
    time: {
        type: String,
        required: true
    },
    organiser: {
        type: String,
        required: true
    },
    attendies: []
}, { timestamps: true} );

但是当我尝试通过邮递员创建会议时,它给了我“无法发布 /api/meeting/post”的错误。 我的参加者字段有什么问题,因为我想将其设置为字符串数组,还有比字符串更好的方法来设置日期和时间字段吗? 我是 nodejs 和 mongoose 的新手

【问题讨论】:

  • 架构看起来很实用。请求的应用程序/邮递员中产生了什么错误?
  • 它返回一些带有 pre 标签的 html 说不能 POST /api/meeting/post

标签: node.js mongoose mongoose-schema


【解决方案1】:

当您没有为array 设置类型时,类型将为Mixed,所以一切都会发生。要专门将其设置为 String 的数组,请使用以下命令:

const meetingSchema = new Schema({
  attendees: [String],
  datetime: Date,
});

日期 + 时间都存储在单个 UTC Date 对象中

【讨论】:

  • 我想输入日期和时间。我不希望它自动生成
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多