@AndreyKawilov

Функция возвращает undefined, как это решить?

index.js
var mysql = require('mysql');
const SQL = require('./functions/SQL.js')
var request = "SELECT * FROM users"
console.log(
    SQL.GetFromSQL(pool, request) //возвращает undefined
)

SQL.js
module.exports = { GetFromSQL }

function GetFromSQL (pool, request) {
    pool.getConnection(function (err, connection) {
        if (err) throw err;
        connection.query(request, function (error, results, fields) {
            connection.release();
            if (error) throw error;
            return results
        });
    });
}
  • Вопрос задан
  • 189 просмотров
Решения вопроса 1
space2pacman
@space2pacman
Просто царь.
module.exports = { GetFromSQL }

function GetFromSQL (pool, request, callback) {
    pool.getConnection(function (err, connection) {
        if (err) throw err;
        connection.query(request, function (error, results, fields) {
            connection.release();
            if (error) throw error;
            callback(results)
        });
    });
}

SQL.GetFromSQL(pool, request, function(results) {
 // lol kek
})


P.S. Называйте методы без тавтологии. GetFromSQL уже понятно что SQL если вызывается у SQL

SQL.get()
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы