【问题标题】:Use interface as type of field in mongoose Schema使用接口作为猫鼬模式中的字段类型
【发布时间】:2022-01-08 00:54:37
【问题描述】:

我正在使用打字稿和猫鼬。我有那个界面:

interface Job {
  jobid: Boolean;
  title: Boolean; 
}

我想创建一个模式,其中一个字段是一个 Jobs 数组:

const workerSchema = new Schema({
  _id: { type: String required: true }
  first_name: { type: String }
  first_name: { type: String }
  jobs: [Job]
})

但我得到错误“作业”仅指一种类型,但在这里被用作一个值。我该如何解决这个问题?

【问题讨论】:

    标签: typescript mongoose


    【解决方案1】:
    const workerSchema = new Schema({
      _id: { type: String required: true }
      first_name: { type: String }
      first_name: { type: String }
      jobs: {type:[Job]}
    })
    

    如果没有工作,请使用类而不是接口

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-15
      • 2021-05-04
      • 2017-12-13
      • 2021-09-25
      • 2018-11-28
      • 2014-12-12
      • 2016-02-07
      • 2020-08-01
      相关资源
      最近更新 更多