0

In module Player, i try to define variables like so:

var database = require('./database');

module.exports = function(req){
  this.req = req;
  this.id = req.session.userId;
  this.name = req.session.username
  this.money = database.getMoney(this.id);
  this.positon = database.getPosition(this.id);
  this.inventory = database.getInverntory(this.id);
  this.gear = database.getGear(this.id);
  this.stats = database.getStats(this.id);
  console.log(this.money);
}

console prints out undefined.

here is the database module

module.exports.getMoney = function(userId){
    con.query(`SELECT player.money FROM player WHERE player.id_user=${userId}`,
    function(err,result,fields){
        if(err) throw err;
        console.log(result);
        return result[0].money;
    });
}

Here console prints out { money : 500 }.

i've also tried doing it with callback like so:

this.money = database.getMoney(this.id,function(data){
   return data;
}
//ofcourse implemented the same callback function in database.js

still undefined

0 Answers0