WDI-SEA / project-2-issues

0 stars 0 forks source link

adding user's saved exercise to db #33

Closed paulinal3 closed 2 years ago

paulinal3 commented 2 years ago

I'm trying to figure out how to add what the user saves to their list. This is the the error I'm not getting: TypeError: {(intermediate value)}.then is not a function

outer.post('/saves', (req, res) => {
    const exerciseData = JSON.parse(JSON.stringify(req.body))
    console.log('this is the exercise data to be saved', exerciseData)
    db.user.findAll({
        where: {
            userId: res.locals.currentUser.id,
        }
        .then(foundUser => {
            console.log('saving exercise to this user\n', foundUser.name)
            foundUser.createExercise({
                name: exerciseData.name,
                bodyPart: exerciseData.bodyPart,
                equipment: exerciseData.equipment,
                muscleTargeted: exerciseData.muscleTargeted,
                exerciseDemo: exerciseData.exerciseDemo
            })
            .then(savedExercise => {
                console.log('exercise details saved to db\n', savedExercise)
            })
        })
    })
})
<form method="POST" action="/exercises/saves">
    <input hidden type="text" name="name" value="<%= name %>">
    <input hidden type="text" name="bodyPart" value="<%= bodyPart %>">
    <input hidden type="text" name="equipment" value="<%= equipment %>">
    <input hidden type="text" name="muscleTargeted" value="<%= targetMuscle %>">
    <input hidden type="text" name="exerciseDemo" value="<%= demoVid %>">
    <button type="submit">Save for Later!</button>
</form>
DoireannJane commented 2 years ago

Hi Paulina, are you still having this issue?

paulinal3 commented 2 years ago

yes

paulinal3 commented 2 years ago

Could still use help on this if/whenever anyone is available 🙏🏼

timmshinbone commented 2 years ago

you're missing a parenthesis and curly brace before you start your .then, your code looks like this:

    db.user.findAll({
        where: {
            userId: res.locals.currentUser.id,
        }
        .then(foundUser => {

and it's trying to run .then inside the findAll, add a parenthesis before .then like this:

    db.user.findAll({
        where: {
            userId: res.locals.currentUser.id,
        }})
        .then(foundUser => {
paulinal3 commented 2 years ago

My bad, I forgot to update my code. I found that error yesterday, here's what it looks like now:

router.post('/saves/', (req, res) => {
    const exerciseData = JSON.parse(JSON.stringify(req.body))
    console.log('this is the exercise data to be saved', exerciseData)
    db.user.findOne({
        where: {id: res.locals.currentUser.id}
    })
    console.log('this is the userId\n', id)
    .then(foundUser => {
        console.log('saving exercise to this user\n', foundUser.name)
        foundUser.createExercise({
            name: exerciseData.name,
            bodyPart: exerciseData.bodyPart,
            equipment: exerciseData.equipment,
            muscleTargeted: exerciseData.muscleTargeted,
            exerciseDemo: exerciseData.exerciseDemo
    })
        .then(savedExercise => {
            console.log('exercise details saved to db\n', savedExercise)
        })
    })
})

but now I've been getting the error id is not defined

timmshinbone commented 2 years ago

get rid of the console log and you should be good to go

paulinal3 commented 2 years ago

that was it! thank you!!