WDI-SEA / project-2-issues

0 stars 0 forks source link

My delete comment button doesnt work #64

Closed anaismveras closed 2 years ago

anaismveras commented 2 years ago

I have two router.deletes in the profile.js because one button is to delete the actual favorited pet and one is to delete the comment but they both kinda over lap each other in some way even though the URL for both of them arent the same.

router.delete('/:animal_id', isLoggedIn, (req, res)=> {
    db.note.destroy({
        where: {id: req.params.animal_id}
    })
    .then(deletedItem => {
        // console.log('you deleted', deletedItem)
        res.redirect(`/profile/${req.params.animal_id}`)
    })
    .catch(error => {
        console.log(error)
    }) 
})

router.delete('/:id', isLoggedIn, (req, res)=> {
    db.favePet.destroy({
        where: {id: req.params.id}
    })
    .then(deletedItem => {
        // console.log('you deleted', deletedItem)
        res.redirect('/profile')
    })
    .catch(error => {
        console.log(error)
    }) 
})
tkolsrud commented 2 years ago

I think you can put a conditional in there that will execute one promise or the other. So if the input name = "comment" .then() destroy comment -- else if the input name = "pet" .then() destrory pet

tkolsrud commented 2 years ago

destroy comment and then .then() ---- sorry obviously the syntax there is wrong, but does the concept make sense?

anaismveras commented 2 years ago

i got it to work another way, I realized I needed a nested .then and I also needed to change the link

router.post('/:animal_id/comments', isLoggedIn, (req, res) => {
    // console.log('this is req.body', req.body)
    db.note.create({
        animalId: req.params.animal_id,
        userId: res.locals.currentUser.id,
        description:req.body.description
    })
    .then(resPost => {
        // console.log('this is resPost', resPost)
        res.redirect(`/profile/${req.params.animal_id}`)
    })
    .catch(error => {
        // console.log(error)
        res.send('invalid comment', error)
    })
})
tkolsrud commented 2 years ago

nice!