简体   繁体   中英

can not get the response from the prisma server

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);
};

Above is my create recipe function.

Here is My route file

router.post('', RecipeController.createRecipe)

** I send this type of data from front end as 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'}]
},

]

But When want to create the recipe in the backend using createRecipes which prisma client provides i cant create the recipe can anybody help me please with this please i am stuck on this.

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"
            }
       }
   }
})

I never used: json(response), I return my result direct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM