3

I am working on a node js application and using DB as mysql what I am trying to do is when I run a query and all data is fetched I want to access the data or store that data to variables for further use

In my controller I am writing this code

exports.login = function(req, res) {
User.fetchUser()
    .then(([rows]) => {

        console.log(rows)
    })
    .catch(err => console.log(err));

}

this one is printing on console like [ BinaryRow { email: '[email protected]', password: 'dheeraj' } ]

in my model class I am executing my fetchUser function

static fetchUser() {
    const email = '[email protected]'
    const password = 'dheeraj'
    let sql = 'SELECT email,password FROM tpconsumer where email = ? and password = ?'
    return db.execute(sql, [email, password]);
}

Now what I am trying to do is get email and password values and store them in variable for further use, or simply how can I use email or my password I want to access them

2
  • 2
    This might help - stackoverflow.com/a/22382596/5893995 Commented Sep 25, 2019 at 8:18
  • I'm not sure what you're asking. Is your problem that you can't extract e-mail and password from [ BinaryRow { email: '[email protected]', password: 'dheeraj' } ]? Commented Sep 25, 2019 at 8:48

2 Answers 2

5

Try to pass params to your fetchUser method

  exports.login = function(req, res) {
        User.fetchUser(email,password)
            .then(([rows]) => {
                 if(rows.length >0)
                 {
                     for(var i=0; i<rows.length; i++){
                         console.log(rows[i].email); 
                         console.log(rows[i].password);
                     }
                  }
                  else{
                   console.log('Nothing to fetch');
                  }

            })
            .catch(err => console.log(err));

And in your Class Model :

static fetchUser(email,password) {
    /*const email = '[email protected]'
    const password = 'dheeraj'*/
    //pass your data dynamically 
    let sql = 'SELECT email,password FROM tpconsumer where email = ? and password = ?'
    return db.execute(sql, [email, password]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The result we get after executing query will be an array. so please try this

user.fetchUser().then(rows => {
      console.log(rows);
      var email = rows[0].email;
      var passw = rows[0].pass;
      console.log("email--",email);
      console.log("passw--",passw);
    }).catch(err => {
      console.log(err)
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.