A
A
AndreyKawilov2020-09-21 23:02:10
Node.js
AndreyKawilov, 2020-09-21 23:02:10

The function returns undefined, how to solve it?

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
        });
    });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Ivanov, 2020-09-21
@AndreyKawilov

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
})

PS Name methods without tautology. GetFromSQL is already clear that SQL if called from SQL
SQL.get()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question