Sunday, 19 March 2017

Mongoose getting array values from another collection individually

Room Schema

var dayData = require('../day-schema');
var roomData = new Schema
(
 {
    room_number     : { type: Number },
    day_of_the_week : [{
                          type: Schema.Types.ObjectId,
                          ref: 'dayData'
                      }]
 },
 {
    collection: 'roomData'
 }
);

module.exports = mongoose.model('roomData', roomData);

Day Schema

    var roomData = require('../room-schema');
    var dayData = new Schema
    (
    { room_number      : {  type: Schema.Types.ObjectId,
                            ref: 'roomData'
                         }, 
      day_of_the_week  : { type: String },
      room_schedule    : [{ class_start: String,
                            class_end: String,
                            class_code: String,
                         }]
    },
    {
       collection: 'dayData'
    });

module.exports = mongoose.model('dayData', dayData);

This is my code for calling the data for each room

var roomDataId = req.params.roomDataId;
    RoomData.findById(roomDataId, 'day_of_the_week')
            .populate('day_of_the_week')
            .exec(function(err, RoomData) {
                if(err) {
                    res.end(err);
                }
                else {
                        res.render('roomData', { RoomData: RoomData});
                }
     });

The output i get is something like this: '{ _id: 58ce60e194ae2b0718544c8f, day_of_the_week: 'Tuesday', __v: 0, room_schedule: [] },{ _id: 58ce60ab94ae2b0718544c8e, day_of_the_week: 'Monday', __v: 0, room_schedule: [] }'

The output I want is something like this:

  • Tuesday
  • Monday

I'm having trouble with trying to get the values of my array individually. When I try to do another findById inside the else, I dont get any output at all. Any help would be much appreciated. Thanks x



via zoenightshade

No comments:

Post a Comment