最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我如何实现此模型

运维笔记admin8浏览0评论

我如何实现此模型

我如何实现此模型

我该如何实现

[{
    "title": "pranam",
    "year": "2016",
    "rating": 9,
    "actors": [
        {
            "name": "Amir",
            "birthday": "16 Aug 1982",
            "country": "Bangladesh"
        },
        {
            "name": "Imran",
            "birthday": "15 Aug 1982",
            "country": "Bangladesh"
        }
    ]
}]

我尝试过这个……

models / actors.js

const Joi = require('joi');
const mongoose = require('mongoose');


const actorSchema = new mongoose.Schema({
    name:{
        type: String,
        required: true,
        min: 5,
        max:50
     },
    birthday:{
        type: String,
        required: true
    },
    country:{
        type: String,
        required: true
    }


 });

models / movies.js

const mongoose = require('mongoose');
const Joi = require('joi');
const actorSchema = require('../models/actors');


const movieSchema = new mongoose.Schema({
    title:{
    type:String,
    required:true,
    min: 5,
    max: 50
},
year:{
    type: String,
    required: true,
    min:2,
    max:4
},
rating:{
    type: Number,
    required: true,
    min:0,
    max:10
},
actors: {
    type: actorSchema,
    required: true
}

});   

routes / movies.js

const { Movie, validate} = require('../models/movies');
const { Actor} = require('../models/actors');
const auth = require('../middleware/auth');
const router = express.Router();


router.get('/', auth, async(req, res)=>{
    const movies = await Movie
    .find({}, { _id:0, __v:0 })
    res.send(movies);
 });  

router.post('/', async(req, res)=>{
   const {error} = validate(req.body);
   if(error) return res.status(400).send(error.details[0].message)

 //May be problem is hare, But I can not solve 
 const actor = await Actor.findById(req.body.actorId);
 if(!actor) return res.status(400).send('Invalid Actors');



 let movie = new Movie({

     title: req.body.title,
     year: req.body.year,
     rating: req.body.rating,
     actors:[{
        name: actor.name,
        birthday: actor.birthday,
        country: actor.country
     }]   
 });


 try {
     movie = await movie.save();
     res.send(movie)
   } catch (ex) {
     console.log("Invalid Movie ");
   }
 });
module.exports =router;

我通过邮递员的POST方法输入此{“标题”:“我讨厌爱情故事”,“评分”:“ 9”,“ actorId”:[“ 5d99ac95f17917117068631b”,“ 5d99ad75c4edd61f98af740b”]}这仅显示通过GET api调用输出的电影中的第一个演员数据,如何在电影中显示更多演员数据。

回答如下:

在问题的上下文中,在routes / movies.js中直到这一点,其他所有东西看起来都很好:

const actor = await Actor.findById(req.body.actorId);

我认为查询不正确,首先,Model.findById()仅接受一个I​​D,而不接受ID数组。其次,您要在此处执行的操作是获取由actorId数组中的ID标识的所有actor,对此,这是一个有效的查询:

const actors = await Actor.find(
  // Filter: fetch all actors whose Id is in the array of ids provided in the request
  { _id: { $in: req.body.actorId } }, 
  // Projection: Include just the name, birthday and country in the response, if any.
  { name: 1, birthday: 1, country: 1 } 
);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论