exports.createRecipe = (req, res, next) => {
const rec = req.body
for (let index = 0; index < rec.length; index++) {
const element = rec[index];
const name = element.name
const description = element.description
const imgUrl = element.imgUrl
const ingredient = element.ingredient
prisma.createRecipes({
name: name,
description: description,
imgUrl: imgUrl,
ingredients:{
create: ingredient
}
})
.then(response=>{
console.log(response)
res.json(response)
}).catch(error=>{
console.log(error)
})
}
// res.status(200).json(recipes);
};
以上是我的创建食谱功能。
这是我的路线文件
router.post('', RecipeController.createRecipe)
**我从前端发送此类数据作为application / json **
[
{
name: 'MY first Recipe',
description: 'Test Recipe',
imgUrl: 'http://www.google.com'
ingredient: [{name: 'Apple', amount: '2'}, {name: 'Tomatoes', amount: '3'}]
},
{
name: 'MY first Recipe',
description: 'Test Recipe',
imgUrl: 'http://www.google.com'
ingredient: [{name: 'Apple', amount: '2'}, {name: 'Tomatoes', amount: '3'}]
},
]
但是,当想使用prisma客户端提供的createRecipes在后端创建配方时,我无法创建配方,有人可以帮助我吗,请坚持。
答案 0 :(得分:0)
return prisma.mutation.createRecipes({ //u missed the mutation
data: { // missed the data {...}
name: name,
description: description,
imgUrl: imgUrl,
ingredients:{
create: {
ingredient: ingredient //ingredient to your "typefieldname"
}
}
}
})
我从没使用过:json(response),我直接返回结果。