0

I'm sorry, I know there are a lot of questions asking this same thing, but I can't find any answers that solve it for me!

I'm trying to use a mongoose populate function to populate the ID of a schema, but it just returns an empty array. The ID's are in the array if I don't use the populate function, but using it seems to delete them.

My routes and models are simple as I'm just trying to learn it, which is more confusing as to why it's wrong! And I thought I followed the tutorial thoroughly....

These are my schema's:

var SeasonSchema = new Schema(
  {
    name: {type: String, required: true, enum: ['Spring', 'Summer', 'Autumn', 'Winter']},
    description: {type: String, maxLength: 300}
  }
);
var FruitSchema = new Schema(
  {
    name: {type: String, required: [true, 'All fruits have names.'], maxLength: 50},
    description: {type: String, maxLength: 300},
    season: [{type: Schema.Types.ObjectId, ref: 'Season', required: true}],
    price: {type: Number, min: 0, max: 9.99, required: true},
    stock: {type: Number, min: 0, max: 999, required: true}
  }
);

And this is the controller I'm trying to get to work: (Simply populating the Fruit's Season field.

exports.fruit_detail = function(req, res, next) {
    Fruit.findOne({name: req.params.name})
    .populate('season')
    .exec(function (err, fruit) {
      if (err) {return next(err);}
      if (fruit==null) {
        var err = new Error('Fruit not found');
        err.status = 404;
        return next(err);
      }
      res.render('fruit_detail', {title: fruit.name, fruit: fruit});
    });
};

Thank you for any help. I'm at my wits end.

2 Answers 2

0

.populate() is an asynchronous method a it needs to be called correctly:

exports.fruit_detail = async function(req, res, next) {
  await Fruit.findOne({name: req.params.name})
    .populate('season')
    .execPopulate(function (err, fruit) {
      if (err) {return next(err);}
      if (fruit==null) {
        var err = new Error('Fruit not found');
        err.status = 404;
        return next(err);
      }
      res.render('fruit_detail', {title: fruit.name, fruit: fruit});
    });
};

Then you will have to import your fruit_deail method and call it asynchronously: await this.fruit_detail(req, res, next)

Sign up to request clarification or add additional context in comments.

6 Comments

Hang on, where am I importing it?
Well, I guess you're exporting fruit_detail somewhere, aren't you?
I think so? In the routing file? router.get('/fruit/:name', fruit_controller.fruit_detail); Sorry, I really thought I was getting to grips with this, but I guess not!
Don't worry :) Where are you defining your fruit_detail controller?
I have an index.js route router.get('/fruit/:name', fruit_controller.fruit_detail); which imports the fruitController which contains the fruit_detail function above.
|
0

Massive face palm emoji.

The problem was the season ID's being referenced were from non-existent seasons. I'd somehow made duplicates when populating the database, and I'd deleted the wrong ones. Feel like I wasted two days of my life, but at least it's sorted!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.