Monday 1 May 2017

Joining two collection in mongoose model giving null value

I am trying to get data from two collection but always getting null value in ref column.

i tried using populate function but everytime i am getting null value in my referenced schema type.

Below is my two collections model code :

Property Model Code :

var mongoose  = require('mongoose');
var schema = mongoose.Schema;
var Users = require('./users.model');
var PropertyType = require('./property_type.model');
var RoomType = require('./room_type.model');
var Admin = require('./admin.model');
var propertySchema = new schema({
    id: {
        type : Number,
        required : true
    },
    host_id: {
        type : schema.Types.ObjectId,
        required :true,
        ref : 'Admin'
    },
    title : {
        type : String,
        required : true
    },
    property_type : {
        type : schema.Types.ObjectId,
        required : true,
        ref : 'PropertyType'
        //foregirn key
    },
    room_type : {
        type : schema.Types.ObjectId,
        required : true,
        ref : 'RoomType'
        //foregin key
    },
});

var Property = module.exports = mongoose.model('Property', 
propertySchema);

module.exports.fetchProperties = function(callback){
    Property.find({}, callback).populate('room_type');
}

Room Type model Code

var mongoose  = require('mongoose');
var schema = mongoose.Schema;
var roomTypeSchema = new schema({
    id : {
        type : Number,
        required : true
    },
    name : {
        type : String,
        required : true
    }
});
var RoomType = module.exports = mongoose.model('RoomType', roomTypeSchema);

Please help me to know where i am wrong in the above code that i am getting null value in room_type schema type.



via vishal

No comments:

Post a Comment