-1

I have a function that supposed to return a first_name and I manipulate it in the javascript file. However, the function returns "undefined". The main idea is to retrieve first_name value from mysql table (people) and compare it with a given name. Here is the function that I use:

function getFirstname() {
connection.query('SELECT first_name FROM people WHERE id = 14', function(err, rows) {
    if (err) throw err;
    return rows;
   });
}
GoGo
  • 2,727
  • 5
  • 22
  • 34

1 Answers1

0

I believe you are missing a return

function getFirstname() {
    return connection.query('SELECT first_name FROM people WHERE id = 14', function(err, rows) {
        if (err) throw err;
        return rows;
    });
}

Alternatively, you can do something like this

function getFirstname() {
    let results;
    connection.query('SELECT first_name FROM people WHERE id = 14', function(err, rows) {
        if (err) throw err;
        results = rows;
    });
    return results;
}
manonthemat
  • 6,101
  • 1
  • 24
  • 49
  • That's the same code that I use and it has return . – GoGo Dec 06 '16 at 23:26
  • 1
    @Karatay - look again it's not the same. Unless the original code you posted is incorrect. Upvoting as looks to be correct to me. – curv Dec 07 '16 at 09:14