想发送我的子类别的标题作为回应。尝试过这种方法但一无所获,谁能告诉我该怎么做。
exports.Subcategory= (req, res) => {
var sub = {}
Subcategory.findById(req.params.id, function (err, subcategory) {
console.log(typeof(subcategory))
res.send({ sub: subcategory.Tittle})
});
};
这是我的收藏
"_id": "5c398e8fd301362158004fd1",
"Description": "value",
"Slug": "value",
"category_id": "5c398ac7d301362158004fcd",
"UserId": "5c3796cad3013606e8001f9c",
"updated_at": "2019-01-12T06:51:59.000Z",
"created_at": "2019-01-12T06:51:59.000Z"
并且要从此“ category_id”中查找“ category”集合中的类别信息并通过响应进行发送
答案 0 :(得分:1)
您必须在猫鼬中使用填充,像这样尝试 在您的SubCategory模式中
const SubCategory = new Schema({category_id:{type:String, ref:'Category'}); // to make the relation with Category model
exports.Subcategory= (req, res) => {
var sub = {}
Subcategory.find({_id:req.params.id}).populate('category_id').exec((err,data)=>{
res.send({ data:data});
});