Monday 3 September 2018

Why mysql query & ldquo; select count (*) from the table & rdquo; back & ldquo; [object object] & rdquo; as a result?

I have a node To create a login function for my website using passport.js. I am NOT using MongoDB to store user info. Instead, I want to use my own AmazonAWS RDS MySQL instance as my database. This means I cannot use Mongoose.
As a result, I need to query for users without Mongoose to verify their credentials in Passport.js. My RDS instance has a table called Users that contains users with just the userName and password. Each record (user) in the table is also assigned its own unique usersObjectID number. I am attempting to query the users based on the user-name that the end-user will input into the login form and matching that user-name with a record in the table.
This is my server-side code:
var connection = mysql.createConnection(
{
    //database connection info
}

connection.connect(function(error)
{
    if(error)
    {
        console.log('MySQL error: '+error);
    }
    else
    {
        console.log('Successfully connected to MySQL database');
    }
});

passport.use(new LocalStrat({
    usernameField: 'username',
    passwordField: 'password'
  },
  function(username, password, done) {
    //query user from DB
    var usersObjectID = -1;
    var tableLength = connection.query("select count(*) from Users");
    console.log("this is the length of the table: "+tableLength);
    for(var i = 0; i < tableLength; i++)
    {
        if(connection.query("select userName from Users where Users.usersObjectID = '"+i+"'") == username)
        {
            console.log(connection.query("select userName from Users where Users.usersObjectID = '"+i+"'"));
            usersObjectID = i;
        }

    }

    if(usersObjectID == -1)
    {
        //user not found
        return done(null, false, {message: "The user is not exist"});
        console.log("user does not exist");
    }

    else if(usersObjectID != -1 && connection.query("select userName from Users where Users.usersObjectID = '"+usersObjectID+"'") != connection.query("select password from Users where Users.usersObjectID = '"+usersObjectID+"'"))
    {
        // if password does not match
        return done(null, false, {message: "Wrong password"});
        console.log("password incorrect");
    }

    else
    {
        // if everything is OK, return null as the error
        // and the authenticated user
        return done(null, user);
        console.log("successfully logged in");
    }
  }
));

This is my post method:
app.post('/login', function (req, res, next) {
var uname = req.body.username;
var pass  = req.body.password;
var rem   = req.body.remember_me;
console.log(uname+", "+pass+", "+rem);

// ask passport to authenticate
passport.authenticate('local', function(err, user, info) {
if (err) {
  // if error happens
  return next(err);
    console.log("err");
}

if (!user) {
  return res.redirect('/login');
    console.log("!user");
}

// if everything's OK
req.logIn(user, function(err) {
  if (err) {
    return next(err);
  }
    console.log("ok");
  return res.redirect('/');
});

})(req, res, next);

});

Here are my questions:
  1. When I query
    var tableLength = connection.query("select count(*) from QCBIMS.Users");
    console.log("this is the length of the table: "+tableLength);
    
    
    I get this result:
    this is the length of the table: [object Object]
    
    
    Why?
  2. Would this be a good way to go about using an RDS instance for my user info and login function?

Encountered the same problem and solved it using the FF steps:
  1. Use alias in the SELECT STATEMENT, like: "SELECT COUNT(*) AS total from QCBIMS.Users"
    Note: Using alias will make it easier to find it later
    
  2. Select the first [key:value] pair from list & stringify as JSON: let resultStr=JSON.stringify(tableLength[0])
    Note: I Used 'tableLength' from the query above. After stringify, result can be like
    this: [{"total":"1541"}]
    
  3. parse result as JSON: let itemPair = JSON.parse(resultStr)
  4. get value using the 'alias' from SQL query: let value = itemPair[0].total
IF YOU WANNA SEE THE CONTENT OF THE [object Object], you can do this: Object.keys(queryResult).forEach(function (key) { console.log(JSON.stringify(queryResult[key])); });

0 comments:

Post a Comment