как я могу реализовать это
[{
"title": "pranam",
"year": "2016",
"rating": 9,
"actors": [
{
"name": "Amir",
"birthday": "16 Aug 1982",
"country": "Bangladesh"
},
{
"name": "Imran",
"birthday": "15 Aug 1982",
"country": "Bangladesh"
}
]
}]
Я пробовал это ......
models / актеры.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
}
});
модели / фильмы.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
}
});
routs / 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 в postman {"title": "Я ненавижу историю любви", "rating": "9 "," actorId ": [" 5d99ac95f17917117068631b "," 5d99ad75c4edd61f98af740b "]} это показывает только первые данные об актерах в фильмах, выводимых вызовом API GET, как я могу показать больше данных об актерах в фильмах.