简体   繁体   中英

Nested type Schema MongoDB for languages

I am creating a MongoDB db in which some information of the models will depend on a configuration file where indicating the languages. So I have and schema used as a type of LanguageSchema (at the bottom) and used in the type declaration but I am getting errors that the type is undefined when running it.

const Schema = mongoose.Schema;
import {LanguagesSchema } from '../config/menutApi.config';

const sectionSchema = new Schema({
    title: { type: LanguagesSchema},
    content: { type: LanguagesSchema},
    image: { type: Schema.Types.String, default: 'image.jpg' },
    gallery: {type: Schema.Types.ObjectId, ref:'Gallery'}
}, { versionKey: false, _id: false });

const ArticleSchema = new mongoose.Schema({
  name: { type: LanguagesSchema },
  tags: { type: [ LanguagesSchema], index: true }, // field level,
  slug: { type: LanguagesSchema, required:true},
  sections: {type: [sectionSchema]}
},{ timestamps: true });


module.exports.getArticleById = function (id: any , callback: any) {
  article_model.findById(id, callback)
}
//Creating our model
export const article_model = mongoose.model("Article", ArticleSchema);

Being LanguageSchema this :

const Schema = mongoose.Schema;
export const LanguagesSchema = new Schema({
        esp: { type: Schema.Types.String}, 
        cat: { type: Schema.Types.String}, 
        eng: { type: Schema.Types.String}
    } , { versionKey: false, _id: false });

And when running it I am getting the following errors:

throw new TypeError('Invalid value for schema path `' + fullPath +
            ^
TypeError: Invalid value for schema path `title.type`, got value "undefined"
    at Schema.add (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/mongoose/lib/schema.js:473:13)
    at Schema.add (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/mongoose/lib/schema.js:507:12)
    at new Schema (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/mongoose/lib/schema.js:130:10)
    at Object.<anonymous> (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/src/models/article.model.ts:5:23)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/src/services/article.service.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/Users/daviniaroca/Documents/programacion/freelance/menutServer/menut_server/node_modules/ts-node/src/index.ts:858:23)

Your article model title field type cannot be your custom type. If you want a field of your model schema a custom schema you have to

const sectionSchema = new Schema({
    title: LanguagesSchema,
});

You can find more information in this answer https://stackoverflow.com/a/39295760/6329540

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM