Mongoose: Embed a model as array of another model -


i have menu items have roles make links them restrictive.

1. rolemodel.js

     const menuroleschema = new mongoose.schema ({           name: { type: string, unique: true}, { _id: true });       exports.menurolemodel = mongoose.model('menurolemodel', menuroleschema, 'menu_role'); 

2. menuitemmodel.js

    const menuitemschema = new mongoose.schema({       ......       roles: [ { type: menurolemodel } ]      }); 

3. menuitemseed.js

   const data = [           {             .....             "roles": [{"name": "rolea"}],           },           {             ....             "roles": [{"name": "rolea"}, {"name": "roleb"}],           }          ] 

my menuitemmodel not populating in database, , naturally no errors provided.

how structure schema seed data laid out roles works?

thanks

this did trick me:

1. rolemodel.js

    (1)export const menuroleschema = new mongoose.schema ({       name: { type: string, unique: true}, { _id: true });      exports.menurolemodel = mongoose.model('menurolemodel', menuroleschema, 'menu_role'); 

2. menuitemmodel.js

     (2)import {menuroleschema} './rolemodel';          const menuitemschema = new mongoose.schema({         ......         roles: [ { type: menuroleschema } ] (3)      }); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -