最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get values from mysql database using node js - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Sep 25, 2019 at 18:13 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Sep 25, 2019 at 8:08 user11543110user11543110 2
  • 2 This might help - stackoverflow./a/22382596/5893995 – Yashwardhan Pauranik 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' } ]? – user10706046 Commented Sep 25, 2019 at 8:48
Add a ment  | 

2 Answers 2

Reset to default 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]);
}

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)
    })
发布评论

评论列表(0)

  1. 暂无评论