最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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',{...})
发布评论

评论列表(0)

  1. 暂无评论