Console.log runs fine, but return undefined

Viewed 32

i'm very new to node.js and i need some help.

I've made a function to retreve data from sqlite3, and i'm trying to pass some value to query... When i console.log the result, all goes well, what am i missig?

This is the function of index.js:

function test(user) {
  return db.getUser("Matt", (err, data) => {
    if (typeof data !== "undefined") {
      console.log(data.stage);
      return data.stage;
    } else {
      console.log("User not found.");
    }
  });
}

And this is the function from functionDB.js:

function getUser(apelidoWa, callback) {
  if (fs.existsSync(dbPath)) {
    let db = new sqlite.Database("./db/dataBase.db", (err) => {
      if (err) {
        console.error(err.message);
      }
      //do stuff
      let sql = `SELECT * FROM users WHERE apelidoWa = ?`;
      db.get(sql, [apelidoWa], (err, row) => {
        if (err) {
          console.error(err.message);
        }
        callback(null, row);
        return row;
        //? console.log(row.userId, row.apelidoWa)
        //: console.log ('user not found')
      });
    });
  } else {
    console.log(
      "No DataBase located, investigate or run checkDb to create Db."
    );
  }
}

When i run the function test i get "undefined" and then the result of the data.stage. I already test lots of things and i kind of lost. any help will be much appreciated. :)

0 Answers
Related