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
2 Answers
Reset to default 5The 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
版权声明:本文标题:javascript - Getting "TypeError: schema.virtual(...).get is not a function" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745645994a2668139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论