0

I have the category schema like this:

var category_article_Schema = new Schema({
    "article_id": { type: String, required: false, unique: false },
    "category": String,
    "article": { type: mongoose.Schema.Types.ObjectId, ref: 'article'},
});

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

Article schema:

var articleSchema = new Schema({
    "title": { type: String, required: true, unique: false },
    "details": String,
    "username": { type: String, required: true, unique: false },
    "postImageUrl": String,
    "url": String,
    "categories": [String],
    "created_at": { type: Date, default: Date.now }
});

var article = mongoose.model('article', articleSchema);
module.exports = article;

When I try to get articles based on category using populate method, I am getting exception: UnhandledPromiseRejectionWarning: Unhandled promise rejection

function getPostByCategory(req, res, next) {
    category_article_model.find({category: req.params.name})
    .populate('article')
    .exec()
    .then(function(err, articlesByCategory) {
        if(err) throw err;
        console.log(articlesByCategory);
    })
}

First of all, what could be the reason for error and why? I tried looking for answers but in each case the issue is different.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
kittu
  • 6,662
  • 21
  • 91
  • 185
  • 2
    add `.catch` after `.then`, you're not handling the exception, the error is clear. – Marcos Casagrande May 19 '18 at 20:05
  • This is not related to mongoose. If you throw errors within a promise context (like you do), a `catch` method call is needed to deal with it, or otherwise the error will be *unhandled*. – trincot May 19 '18 at 20:08
  • @MarcosCasagrande I added catch but the article is not populated but instead category table is returned. Any idea? – kittu May 19 '18 at 20:09
  • @MarcosCasagrande There is no error but instead of article being populated, category is returned – kittu May 19 '18 at 20:11
  • @MarcosCasagrande `{ _id: 5b0008ce8787890004989df2, category: 'sample category', article_id: '5b0008ce8787890004989df1', __v: 0 } ]` is returned – kittu May 19 '18 at 20:12
  • `then` takes a callback with a *single* argument (or two callbacks). – Bergi May 19 '18 at 20:14
  • @Bergi Yes I removed extra argument from `then` and added a `catch` – kittu May 19 '18 at 20:15
  • @Satyadev You can also drop the `catch` completely if all the callback does is to re-`throw` the error – Bergi May 19 '18 at 20:16

1 Answers1

1

Refactor

function getPostByCategory(req, res, next) {
    category_article_model.find({category: req.params.name})
    .populate('article')
    .exec()
    .then(function(err, articlesByCategory) {
       if(err) throw err;
       console.log(articlesByCategory);
    })
}

to this

function getPostByCategory(req, res, next) {
    category_article_model.find({category: req.params.name})
   .populate('article')
   .exec()
   .then(function(articlesByCategory) {
      console.log(articlesByCategory);
   })
   .catch(function (err){
      throw err; // or handle it
   })
}
shanks
  • 912
  • 10
  • 23
  • I added `catch` but the article is not populated but instead category table is returned. Any idea? – kittu May 19 '18 at 20:16
  • @Satyadev can there be more than 1 article in the category? if so try turning it into an array of articles in the schema definition. `articles: [{ type: mongoose.Schema.Types.ObjectId, ref: 'article'}]` – shanks May 19 '18 at 20:26
  • Changed it to array type but still returns empty array. I have posted a new question here: https://stackoverflow.com/questions/50429288/unable-to-populate-articles-in-mongoose. I am new to mongodb/mongoose. – kittu May 19 '18 at 20:39