Home > Blockchain >  send response to client in node js express is not working
send response to client in node js express is not working

Time:02-21

I am trying to send back response to the client but it seems it is not working, I don't know why, because after all I am not getting any errors.

here is my code:

exports.login = function (req, res, next, con) {
    if (!req || !res || !next || !con) return res.status(500).set({ msg: 'null variables' });

    var email = req.body.email, password = req.body.password;
    if (!email || !password) return res.status(500).set({ msg: 'null values' });
    var format = /^[!#$%^&*()_ \-=\[\]{};':"\\|,.<>\/?]*$/;

    if (!email.match(format)) {
        query1 = "SELECT `id`, `password` FROM `ceo`.`users` WHERE `email`= '"   email   "'"
        // check users exists with this email
        con.query(query1, (err, result) => {
            const bcrypt = require('bcrypt');
            var jwtGenerator = require('./../../classes/JWT/JWTgenerator')

            if (err) return res.status(500).set({ msg: 'error checking' })
            if (result.length === 0) return res.status(500).set({ msg: 'wrong email' })
            // check password
            bcrypt.compare(password, result[0].password, function (err) {
                if (err) return res.status(500).set({ msg: 'wrong password' })

                // update refreh token in database
                const rtoken = jwtGenerator.generateRefreshToken({ id: result[0].id });
                var queryUpdate = "UPDATE `token` SET `_token` = '" rtoken "' WHERE `token`.`id` = '" result[0].id "';"
                con.query(queryUpdate, (err) => {
                    if (err) return res.status(500).set({ msg: 'error in update' })
                    res.status(200).set({
                        msg: {
                            accessToken: jwtGenerator.generateAccessToken({ id: result[0].id }),
                            refreshToken: rtoken
                        }
                    })
                });
            });

        });
    } else {
        return res.status(500).set({ msg: 'special chars' })
    }

}

CodePudding user response:

I think you have to use .send instead of set !!

Like this :

res.status(500).send({ msg: 'null values' });
  • Related