WDI-SEA / project-2-issues

0 stars 0 forks source link

Adding favorites to user #29

Closed DagemBefikadu closed 2 years ago

DagemBefikadu commented 2 years ago
router.post('/:id',  (req,res) => {
    db.usercoffee.create({
        userId: res.locals.currentUser.id,
        coffeeId: req.params.id
    })
    .then(favCoffee => {
        console.log(favCoffee)
        res.redirect('/profile')
    })
    .catch(error => {
        console.log(error)
      })
})
  <form action="/coffees/<%= coffeeDescription.id %>" method="POST">
    <input
      hidden
      type="text"
      name="name"
      value="<%= coffeeDescription.name %>"
    />
    <button class="btn btn-light" type="submit">Favorite this Coffee</button>
  </form>

The error that i got:

TypeError: Cannot read property 'create' of undefined

TaylorDarneille commented 2 years ago

You need to create a coffee, not a usercoffee

TaylorDarneille commented 2 years ago

(also, you want to do a findOrCreate, not create)

TaylorDarneille commented 2 years ago

then after you've found or created, you can associate the coffee with the user

DagemBefikadu commented 2 years ago
router.post('/:id',  (req,res) => {
    db.coffee.findOrCreate({
        userId: res.locals.currentUser.id,
        coffeeId: req.params.id
    })
    .then(favCoffee => {
        console.log(favCoffee)
        res.redirect('/profile')
    })
    .catch(error => {
        console.log(error)
      })
})
TaylorDarneille commented 2 years ago

lmk how this goes