admin管理员组

文章数量:1434921

I get this error while I try to define my schema.

Error:

node_modules/mongoose/lib/plugins/idGetter.js:12
    schema.virtual('id').get(idGetter);

TypeError: schema.virtual(...).get is not a function
    at module.exports (/Users/g.paradiso/dev/albumin-diet/node_modules/mongoose/lib/plugins/idGetter.js:12:26)

Schema:

export const albumSchema = new Schema({
  id: {
    spotify: String
  },
  tags: [{ type: Schema.Types.ObjectId, ref: "Tag" }],
}, { timestamps: true });

I get this error while I try to define my schema.

Error:

node_modules/mongoose/lib/plugins/idGetter.js:12
    schema.virtual('id').get(idGetter);

TypeError: schema.virtual(...).get is not a function
    at module.exports (/Users/g.paradiso/dev/albumin-diet/node_modules/mongoose/lib/plugins/idGetter.js:12:26)

Schema:

export const albumSchema = new Schema({
  id: {
    spotify: String
  },
  tags: [{ type: Schema.Types.ObjectId, ref: "Tag" }],
}, { timestamps: true });
Share Improve this question asked Dec 1, 2018 at 10:27 gianlucaparadisegianlucaparadise 1,77322 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The error was raised because I had a field called id that probably was overriding the internal _id field.

I resolved changing my schema in:

export const albumSchema = new Schema({
  publicId: {
    spotify: String
  },
  tags: [{ type: Schema.Types.ObjectId, ref: "Tag" }],
}, { timestamps: true });

I found that this is mostly caused by defining a virtual field name that is already defined in the schema. In your case id is already defined in the schema, changing the name of the defined property in the schema or changing the virtual field name should work.

For example

export const albumSchema = new Schema({
  // changing from id to maybe album_id
  id: {
    spotify: String
  },
  tags: [{ type: Schema.Types.ObjectId, ref: "Tag" }],
}, { timestamps: true });

// changing the virtual field name id to maybe spotify_id will also work
schema.virtual('id',{...})

本文标签: javascriptGetting quotTypeError schemavirtual()get is not a functionquotStack Overflow