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.