Collins Orlando
Collins Orlando

Reputation: 1681

Why am I unable to spread an object in my mongoose model?

This might be a silly question but why on earth am I getting an "Unexpected token" error for the following code snippet? Bear in mind it is a mongoose model.

Error message

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 model

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

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

Upvotes: 0

Views: 935

Answers (1)

codeofnode
codeofnode

Reputation: 18629

Spread operator is used as parameters from array.

You might want to use this, instead

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

Upvotes: 1

Related Questions