最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Getting "TypeError: schema.virtual(...).get is not a function" - Stack Overflow

matteradmin8PV0评论

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',{...})
Post a comment

comment list (0)

  1. No comments so far