我从以下代码中收到此错误“ UnhandledPromiseRejectionWarning:TypeError:无法将对象转换为原始值”,我找不到该代码的实际问题。当我尝试通过API发布电影时,出现此错误。我正在使用mongodb并表达用于创建CRUD应用程序。
const joiSchema = Joi.object().keys({
category: Joi.string().required(),
})
const joiSchemaMovie = Joi.object().keys({
category: Joi.string(),
movies :[{
movieName: Joi.string().required()
}]
})
const joiSchemaMovies = Joi.object().keys({
movieName: Joi.string().required()
})
const categorySchema = new mongoose.Schema(joigoose.convert(joiSchema))
const movieSchemaMain = new mongoose.Schema(joigoose.convert(joiSchemaMovie))
var Category = mongoose.model("Genres", categorySchema);
var Movie = mongoose.model("Movie", movieSchemaMain);
function validateMovieName(movie){
return Joi.validate( movie , joiSchemaMovies)
}
app.post('/api/category/:category',(req,res) =>{
const result = validateMovieName(req.body);
if(result.error){
res.status(400).send(result.error.details[0].message);
return ;
}
Category.findOne({category:req.params.category} , (err , item ) => {
if(item !== null){
Movie.findOne({category : req.params.category },(err , item1) => {
if(item1 !== null){
let movieData = new Movie();
// movieData.movies = [];
movieData.movies = [(req.body)];
movieData.category = (req.params.category) ;
console.log("movieData",movieData)
movieData.save().then(item => {
res.send(item);
}).catch(err =>
res.status(400).send("unable to save to database");
})
}
})
}else{
res.send("Category Not exits...")
return ;
}
});
});
答案 0 :(得分:0)
const joiSchemaMovie = Joi.object().keys({
category: Joi.string(),
movies:Joi.array().items({
movieName: Joi.string().required()
})
})
尝试