redAce
redAce

Reputation: 1808

How to select rows[0] while inserting in node-mysql?

I'm fairly new to nodejs and callbacks. Here is my problem, using passportJS's LocalStrategy and node-mysql :

exports.register = new LocalStrategy(strategyOptionsRegister, function(req, username, password, done) {

    //get data from the request
    var data = {
        username: username,
        email: req.body.email,
        password: password
    };
    console.log('data : ', data);

    //Hash passwords
    bcrypt.genSalt(10, function(err, salt) {
        if (err) return next(err);

        bcrypt.hash(password, salt, null, function(err, hash) {
            // Store hash in your password DB.
            if (err) return next(err);

            data.password = hash;

            //insertion 
            connection.query('INSERT INTO USERS SET ?', data, function(err, rows) {
                if (err) {
                    console.log(err);
                    return next("Mysql error, check your query");
                }
                return done(null, rows[0]);
            });
        });
    });
});

I'm trying to return rows[0] containing all the data, but i don't know how should i implement the SELECT command ? Is it before or after the callback for the insertion ? For the moment, rows[0] is naturally undefined.

Upvotes: 0

Views: 1971

Answers (2)

redAce
redAce

Reputation: 1808

Here is my solution :

exports.register = new LocalStrategy(strategyOptionsRegister, function(req, username, password, done) {

    //get data from the request
    var data = {
        username: username,
        email: req.body.email,
        password: password
    };

    //Hash passwords
    bcrypt.genSalt(10, function(err, salt) {
        if (err) {
            return done(err);
        }

        // Store hash in your password DB.
        bcrypt.hash(password, salt, null, function(err, hash) {
            if (err) {
                return done(err);
            }

            data.password = hash;

            //insertion 
            connection.query('INSERT INTO USERS SET ?', data, function(err, rows) {
                if (err) {
                    return done(null, false, {
                        message: 'Mysql error, check your query !'
                    });
                }
                // to return all the info in rows[0]
                connection.query('SELECT * FROM USERS WHERE email = ?', data.email, function(err, rows) {
                    if (err) {
                        return done(null, false, {
                            message: 'Email not found !'
                        });
                    }
                    return done(null, rows[0]);
                });
            });
        });
    });
});

Upvotes: 0

Dimony
Dimony

Reputation: 25

what about using async.waterfall?

I solve similar problem.

  1. insert query
  2. get auto_incremnet number from rows[0]
  3. select query

website of async here https://github.com/caolan/async#waterfall

Also, as bcrypt is asyncronous,

data,password = hash

this code doesn't work properly.

I want to execute same type of code for yours but I can't. So, I use bcrypt in Sync and pass the hash to query.

Upvotes: 0

Related Questions