0

Im trying to create a FoodItem Schema In which i kept measureUnit as an object reference from another page's model.

This is MeasureTypesSchema where the measureUnit is present. MeasureTypes is a embedded array document inside MetricsSchema. I am exporting both MeasureTypesSchema's model (MeasureTypes) and MetricsSchema's model (Metrics).

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var MeasureTypesSchema = new mongoose.Schema({                
measureUnit:{type:String,required:true},                 
code:{type:String,required:true}                        

});
var MeasureTypes = mongoose.model('MeasureTypes', MeasureTypesSchema);
module.exports = MeasureTypes;



var MetricsSchema = new mongoose.Schema({                      
organisation:{type: Schema.Types.ObjectId, ref: 'Organisation'},
MeasureTypes:[MeasureTypesSchema]

});
var Metrics = mongoose.model('Metrics', MetricsSchema);
module.exports = Metrics;

This is FoodItemSchema where i kept measureUnit as an object reference from MeasureTypes model present in another page

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var FoodItemSchema = new mongoose.Schema({

measureUnit:{type: Schema.Types.ObjectId, ref:'MeasureTypes',required:true},           

});

var  FoodItem = mongoose.model('FoodItem', FoodItemSchema);
module.exports =  FoodItem;

I need to get the details of measureUnit when i populate, how to populate so that i get the details of measureUnit.

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
SUNDARRAJAN K
  • 2,237
  • 2
  • 22
  • 38

2 Answers2

0

I tried populating measureUnit from FoodItem and got a null.

FoodItem.findById(foodId).lean().populate({path:'measureUnit'}).exec(function (err, food)                           
{ console.log(food);} 

The result i got for this populate is

{
"_id": "5440d008b9215ace25587835",

"measureUnit": null,

"__v": 0
}         
SUNDARRAJAN K
  • 2,237
  • 2
  • 22
  • 38
0

It sounds as though your models are spread across different files. In principle this is fine, but the entire model should brought together so that the whole application has access to it.

In applications main file (typically app.js) you bring in your database connection and model

var db = require('./model/db')

In /model/db.js you can connect to your database and import a number of schema files:

// Bring Mongoose into the app
var mongoose = require( 'mongoose' );

// Build the connection string
var dbURI = 'mongodb://localhost/ConnectionTest';

// Create the database connection
mongoose.connect(dbURI);

// BRING IN YOUR SCHEMAS & MODELS
// For example
require('./metrics');
require('./fooditems');

In each of your schema files you require Mongoose, define your schemas and build model, for example:

var mongoose = require('mongoose');

var MeasureTypesSchema = new mongoose.Schema({                
  measureUnit:{type:String,required:true},                 
  code:{type:String,required:true}                        
});

mongoose.model('MeasureTypes', MeasureTypesSchema);

I haven't tried defining referenced schemas in separate files; if this continues to cause problems I'd try putting them all into the same file.

Finally on pages you want to use your models, require Mongoose and import the model again.

var mongoose = require( 'mongoose' ),
    MeasureTypes = mongoose.model('MeasureTypes');

For more info and background take a look at my blog post on Mongoose connections and usage: http://theholmesoffice.com/mongoose-connection-best-practice/

Simon Holmes
  • 303
  • 1
  • 6