javascript - 为什么我无法在我的 Mongoose 模型中传播对象?

标签 javascript node.js mongodb mongoose mongoose-schema

这可能是一个愚蠢的问题,但是为什么我会收到以下代码片段的“意外 token ”错误?请记住,这是一个 Mongoose 模型。

错误消息

SyntaxError: D:/Coding/Species Project/backend/models/species.js: Unexpected token (15:2)
  13 |  },
  14 |  organism: {
> 15 |          ...shared,
     |          ^
  16 |          enum: ["Plant", "Animal", "Other"],
  17 |  },
  18 |  taxonomy: {

Mongoose 模型

const shared = {
    type: String,
    required: true,
}

const SpeciesSchema = new Schema({
    name: {
        common: shared,
        scientific: shared,
    },
    organism: {
        ...shared,
        enum: ["Plant", "Animal", "Other"],
    },
    ...,
}

最佳答案

扩展运算符用作数组的参数。

您可能想使用它

const SpeciesSchema = new Schema({
    name: {
        common: shared,
        scientific: shared,
    },
    organism: Object.assign({}, shared, enum: ["Plant", "Animal", "Other"]},
    ...,
}

关于javascript - 为什么我无法在我的 Mongoose 模型中传播对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936440/

相关文章:

mongodb - 在 Meteor MongoObservable 中获取插入的文档 ID

javascript - Sinon 监视 WebSocket

javascript - 如何在我的网页上设置倒计时器

Node.js Redis 获取 HashMap 中的所有值

node.js - 在 Windows 10 上安装 Node.js(和 npm)

java - 带有 Java 异步驱动程序的 MongoDB : $lookup always empty

javascript - 在 Yesod 处理程序的代码中使用 JS 变量

javascript - 使用正则表达式将格式化的货币金额转换为 double 值

javascript - 有哪些工具可用于在 Node.js 中操作 HTML?

linux - 无法写入/sys/kernel/以在 OVH CentOS 7 上禁用 MongoDB 的透明大页面 (THP)