After trying to populate an Array of IDs mongoose is just returning an empty Array instead of the populated Array. I also tried to use the findById and find methods.

The Route is working.

in my Controller file:

var Restaurant = require('../../shared/models/restaurant');
var objectID = require('mongodb').ObjectID

exports.get_Restaurant_detail = (req, res, next) => {
    if(objectID.isValid(req.params.id)){
        Restaurant.findOne({"_id": req.params.id})
        .populate("dishes")
        .exec( (err, restaurant) => {
            if(err) return res.send("error");
            res.json(restaurant);
        });
    } else {
        res.status(400).json({"status": "Error"})
    }

}

in my Router file:

//GET detailed information about a specific restaurant
router.get('/restaurants/detail/:id', read_controller.get_Restaurant_detail);

The Restaurant Schema:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var RestaurantSchema = new Schema(
  {
    name : {type: String, required: true, max: 100},
    description: {type: String, required: true, max: 2000},
    image: {type: String, required: false, max: 100},
    category: [{type: String, required: true, max: 50}],
    location: {type: String, required: true, max: 50},
    adress: {type: String, required: true, max: 50},
    show: {type: Boolean, required: true, default: false},
    open: {
      from: {type: Number, required: true, max: 24},
      to: {type: Number, required: true, max: 24},
    },
    dishes: [{type: Schema.Types.ObjectId, required: true, ref:'Dish'}]
  },
  {
  toObject: {
    virtuals: true
  },
  toJSON: {
    virtuals: true
  }
});

module.exports = mongoose.model('Restaurant', RestaurantSchema);

The Dish Schema

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var DishSchema = new Schema(
  {
    name : {type: String, required: true, max: 100},
    price : {type: Number, required: true, max: 500},
    description: {type: String, required: true, max: 2000},
    picture: {type: String, required: false, max: 100},
    category: {type: String, required: true, max: 50},
    restaurant: {type: Schema.Types.ObjectId, required: true, ref:'Restaurant'}
  },
  {
  toObject: {
    virtuals: true
  },
  toJSON: {
    virtuals: true
  }
});

module.exports = mongoose.model('Dish', DishSchema);

(The Schemas are in two different files)

0 Answers