问题描述
我想知道代码有什么问题,为什么代码不起作用。
我喜欢这样的输出:
{
"seasons":
[{
"_id": "dhuhuasuhru8484r9r9ur","number": 2,"year": 2009,"episodes": [{
//only the specific episode,if I type 5 in the url,I must receive 5th episode
and not all episodes
"id": "ruuhjbrrjrur9f887f88fds898","title": "universal stone","number": 5,"releasedOn": "2009-11-05T16:30:00.002Z","description": "Us"
"cast": [{
"_id":"jfsijfju8u435ju35uh","fullName":"Joseph Sherlington","birthDate": "2012-02-10T15:00:00","genre":"male","nationality":"french"
},{
"_id":"uhhgb545h4nhhyuh","fullName":"Mathhiias toryn","birthDate": "2012-05-10T16:00:00","genre": "male","nationality":"german"
}],"director":{
"_id":"hjrhbh4h443h4h","fullName": "Alexis Sanders","nationality": "american"
}
}]
}
]}
这是我的电视节目 routes.js:
router.get("/showid/:tvShowId/episodenumber",tvShowsCtrl.getTvShowEpisode);
模型:
const tvShowSchema = new Schema({
name: String,year: Number,country: String,seasons: [{
number: Number,episodes:[{
title: String,number: Number,releasedOn: Date,description: String,cast:[
{type: Schema.Types.ObjectId,ref:'actors'
}],director:{
type: Schema.Types.ObjectId,ref:'directors'
}
},{timeStamps: true,versionKey: false
}
]
}]
});
这是不填充也不显示特定季节的特定剧集时的电视节目控制器方法。我在 url 中使用一个 id 和查询以避免重复的 id:
export const getTvShowEpisode = async(req,res) =>{
try{
const { tvShowId } = req.params;
const { seasonId } = req.params;
const { episodeId } = req.params;
let selectedEpisode = null;
const tvShowEpisode = await TVShow.aggregate([
{ $match:{
"_id": Types.ObjectId(tvShowId),/* "seasons._id": Types.ObjectId(seasonId),"seasons.episodes._id": Types.ObjectId(episodeId),*/
"seasons.number": req.query.seasonnumber,"seasons.episodes.number": req.query.number,}},// Does not populate
{$lookup: {from:'actors',localField: 'seasons.episodes.cast',foreignField:
'_id',as: 'actors'}
},//Does not populate
{$lookup: {from:'directors',localField: 'seasons.episodes.director',foreignField: '_id',as: 'director'}},{$unwind: '$seasons'},{$match: {"seasons._id": Types.ObjectId(seasonId)}},{$project: {"_id": Types.ObjectId(tvShowId),"episode": "$seasons.episodes"}},]);
if (tvShowEpisode) {
res.send(tvShowEpisode);
} else {
res
.status(404)
.send({ mensaje: `tv show episode not found` });
}
}
catch(error){
console.log(error);
res.status(500).send({message: `error: \n\n${error}`});
}
};
我在 postman 上得到的真正输出是虽然“200 OK Status”,但它返回的是空的并且不填充演员和导演。
我想知道我能做什么。没有console.log(err)的错误信息。
怎么了?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)