node.js - Can't make populate in mongoose returning null -
I am trying to create a FoodItem schema in which I put the measurement page in context from the model of another page is.
This is MeasureTypesSchema where there is an embedded array document inside the Measurement Types Metricscake, where measureUnit exists. I am exporting both the Measurement TypeSesquamation model (Measurement Type) and Models of MetricsScheme (Metrics).
var requires mongoose = 'magos'); Schema = mongos Sema; Var Majertpsamsama = New Mongoose Sema ({measureUnit: {type: string, required: true}, code: {type: string, required: true}}); Var marget types = mongos Model ('measurement type', measurement typesskema); Module.exports = Type Measurements; Var MetricsSamy = New Mongoose Sema ({Organization: {Type: Schema Type I. Object ID, Ref: 'Organization'}, Measurement Type: [MajorTypeSema]}); Var metrics = mongos Model ('matrix', metrics sama); Module.exports = Metrics;
This food is ITEMSCHA, where I have placed the UIITIT as an object reference from the MuzterTape model in another page
var mongoose = require ('mongoose '); Schema = mongos Sema; Var FoodITschchi = New Mongoose Chima ({measureUnit: {type: schema type. Object ID, ref: 'measurement type', required: true},}); Var FoodItim = Mongozos Model ('Food Atom', Food Eatamashama); Module Exports = Food Items;
I need to get the measurement details when I populate, how to populate that I get measureUnit details.
It seems that your models are spread across different files. In principle, it's okay , But the whole model should be brought together so that the whole application should be used.
You bring your database connection and model to the main file (usually in app.js) in applications
var db = require ('./- model / db' )
In the / model / db.js you can connect to your database and many can import schema files:
// to Mongoose Bring Apo in Mongo = Essential ('Mongos'); // create connection string string var dbURI = 'mongodb: // localhost / ConnectionTest'; // Create database connection mongoose.connect (dbURI); // in your series & amp; Amp; Model // is required for example ('./metrics'); ('./ fooditems') is required;
In each schema file you need a Mongoise, for example: Define your schema and build model:
var mongoose = require ('mongoose' ); Var Majertpsamsama = New Mongoose Sema ({measureUnit: {type: string, required: true}, code: {type: string, required: true}}); Mongos Model ('Measurement Type', Measurement Schemes Schema);
I have not tried defining the referenced schema in different files; If this problem persists, then I will try to put them all in one file.
Finally, the pages on which you want to use your model, need the Mongoos and import the model again.
var mongoose = requires ('mangos'), measurement type = mongosse MODEL ('measurement type');
For more information and background, take a look at my blog post on Mongoose Connection and Use:
Comments
Post a Comment