Tengo el esquema de categoría como este:
var category_article_Schema = new Schema({ "article_id": { type: String, required: false, unique: false }, "category": String, "articles": [{ type: mongoose.Schema.Types.ObjectId, ref:'article'}], }); var category_article_Schema = mongoose.model('category_article_Schema', category_article_Schema); module.exports = category_article_Schema;
Esquema del artículo:
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;
Cuando bash rellenar artículos en la siguiente función, solo obtengo la identificación y el nombre de la categoría pero no los artículos completados.
function getPostByCategory(req, res, next) { category_article_model.find({category: req.params.name}) //sports .populate('articles') .exec() .then(function(articlesByCategory) { console.log(articlesByCategory); }) .catch(function(err){ console.log('err ', err); }) }
Todos los datos que tengo en la colección de article
son los siguientes:
`{ "_id": { "$oid": "5b0008ce8787890004989df1" }, "categories": [ "sports", "health" ], "title": "sample", "details": "sample ", "created_at": { "$date": "2018-05-19T11:21:50.837Z" }, "__v": 0 }, { "_id": { "$oid": "5b0087646dda9600049a9a27" }, "categories": [ "sports" ], "title": "sample3333", "details": " sample3333", "created_at": { "$date": "2018-05-19T20:21:56.126Z" }, "__v": 0 }`
Y la colección category_article_schema
tiene:
{ "_id": { "$oid": "5b0087646dda9600049a9a28" }, "category": "sports", "article_id": "5b0087646dda9600049a9a27", "__v": 0 }
Pero los datos devueltos son una matriz vacía de artículo:
[ { articles: [], _id: 5b0008ce8787890004989df2, category: 'sports', article_id: '5b0008ce8787890004989df1', __v: 0 } ]
No estoy seguro de cuál podría ser el problema?