Home > Back-end >  node/express duplicate variable issue
node/express duplicate variable issue

Time:08-09

I've got some checkuser middleware that stores the user entry when a JWT is verified. However, when I include it in my routes and try to console.log(res.locals.user.username) I get the username logged twice. When I'm trying to store this username in some JSON, its creating a seperate JSON with {username: ___} that is causing issues in Mongoose. Help would be appreciated, thanks.

const checkUser = async (req, res, next) => {
    const token = req.cookies.jwt

    if (token) {
        jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, async (err, decodedToken) => {
            if (err) {
                res.locals.user = null
                console.log(err.message)
                next()
            }
            else {
                const user = await User.findById(decodedToken.id)
                res.locals.user = user
                next()
            }
        })
    }
    else {
        res.locals.user = null
        next()
    }
}
app.post('*', checkUser)  //all routes
app.post('/newuser', requireAuth, async (req, res) => {
    let user = res.locals.user
    console.log(res.locals.user.username)
    let user_req_body = req.body
    let starter_workout = {}
    starter_workout.username = user.username
    user_req_body.username = user.username
    if (user_req_body.FitnessMode == 'cardio') {
        starter_workout.workout = cardio
        starter_workout.workout_name = 'Default Cardio'
    }
    else if (user_req_body.FitnessMode == 'hypertrophy') {
        starter_workout.workout = hypertrophy
        starter_workout.workout_name = 'Default Hypertrophy'
    }
    else if (user_req_body.FitnessMode == 'powerlifting') {
        starter_workout.workout = powerlifting
        starter_workout.workout_name = 'Default Powerlifting'
    }
    else if (user_req_body.FitnessMode == 'calisthenics') {
        starter_workout.workout = calisthenics
        starter_workout.workout_name = 'Default Calisthenics'
    }
    const user_info = new userInfo(user_req_body)
    const workout_info = new routines(starter_workout)
    /*
    await user_info.save()
        .then(resp => console.log(resp))
        .catch(err => console.log(err))
    await workout_info.save()
        .then(resp => console.log(resp))
        .catch(err => console.log(err))

This code will send duplicated data to MongoDB. Also worth noting that this happens only with app.use(express.json()). I guess thats where I would need help with some work-around. Thank you.

CodePudding user response:

If console.log(res.locals.user.username) inside of this:

app.post('/newuser', ...)

is outputting twice, then that's because your server is getting two posts requests to /newuser. A common reason for that is if this comes from a <form> in your web page that you are also sending an ajax call from javascript in the page. If you don't properly use e.preventDefault() to prevent the default post that the browser does automatically, then you will get duplicate post requests. To look into that more, we'd have to see how the /newuser request comes from the web page.

CodePudding user response:

app.post('/newuser', ...);

add this

  • Related