'use strict';
const sqlite = require('sqlite3').verbose();
const db = new sqlite.Database('database.db');
module.exports.register = function (from) {
db.all("SELECT * FROM users WHERE tele_id = ?", [from.id], function (err, rows) {
if (rows.length < 1) {
if (from.last_name === undefined) {
from.last_name = null;
}
if (from.username === undefined) {
from.username = null;
}
db.run("INSERT INTO users (tele_id, first_name, last_name, username) VALUES (?, ?, ?, ?)", [from.id, from.first_name, from.last_name, from.username]);
}
});
};
module.exports.balance = function (tele_id) {
let balance;
db.get("SELECT * FROM users WHERE tele_id = ?", [tele_id], function (err, result) {
console.log(result.balance); // 100
balance = result.balance; // undefined
});
return balance;
};
Функция balance возвращает undefined, а должна возвращать баланс. console.log из callback'a возвращает 100 а не undefined.