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

I get this error while I try to define my schema.Error:node_modulesmongooselibpluginsidGetter.js:12

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645994a4637974.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信