无法从prisma服务器获得响应

时间:2020-02-28 15:09:01

标签: node.js express prisma

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在后端创建配方时,我无法创建配方,有人可以帮助我吗,请坚持。

1 个答案:

答案 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),我直接返回结果。