bind value of callack to property of instance nodejs
Clash Royale CLAN TAG#URR8PPP
bind value of callack to property of instance nodejs
I want to permanently store a result of a callback function, here is the code:
class constants
constructor()
this.acquire()
acquire()
this.last_refresh = new Date().getDate();
require('./db_slave').then(conn =>
return conn.query("SELECT table_name FROM information_schema.tables where table_schema='gen'").then(result =>
this.tables = result
console.log(result)
);
).catch(err =>
// handle error here
);
tables()
this.refresh()
return this.tables
refresh() if (new Date().getDate() != this.last_refresh) this.acquire()
module.exports = constants
but this is obviously not working because this.tables = result
doesnt do anything...
this.tables = result
Here is the file that is being called by this promise:
var mysql = require('promise-mysql');
var db = require('./db')
module.exports = mysql.createConnection(
host: db.host,
user: db.user,
password: db.password
).then(conn =>
console.log("connected to the DB");
return conn;
).catch(err =>
console.log("error connecting to the DB", err);
throw err;
);
How do i permanently store the result in that callback?
here is how i used these:
var db_constants = require('./database_service/db_constants')
let cons = new db_constants()
console.log(cons.tables())
this.tables = result
this.tables
results
where do you instantiate an instance of
db_tables
(e.g. const x = new db_tables;
and where do you execute acquire
method on that instance? (e.g. x.acquire()
)– Jaromanda X
Aug 8 at 3:51
db_tables
const x = new db_tables;
acquire
x.acquire()
check the edits for more info
– user10111623
Aug 8 at 3:54
this.tables = result
will mean tables()
function will be overwritten and no longer be a function! you can't have a method and a property with the same name– Jaromanda X
Aug 8 at 4:00
this.tables = result
tables()
in your code, replace the last line with
cons.then(tables => console.log(tables))
- there's no way to get around asynchrony– Jaromanda X
Aug 8 at 4:47
cons.then(tables => console.log(tables))
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what makes you think
this.tables = result
isn't doing anything? it surely will setthis.tables
to the value ofresults
once the query is executed– Jaromanda X
Aug 8 at 3:49