N
N
NeTLeaDeR2019-09-29 16:09:17
Node.js
NeTLeaDeR, 2019-09-29 16:09:17

Why does return from a module return undefined?

Hello.
There is a code:
File1:

function doQuery(sql, array) {
            pool.getConnection(function (err,mysql){
                mysql.query(sql, array, function(err, res){
                    if(err) {
                        return `Error: ${err}`;
                    }
                    else {
                        return res;
                    }
                });
                mysql.release();
            });
}

module.exports = {
    query: doQuery
}

File2:
const db = require('./db-connect.js');

var test = db.query("SELECT * FROM users WHERE id = ?", [32]);
console.log(test);

As a result, undefined is displayed in the console.
Is there any way to fix this without using async function and Promise?
If instead of return to File1 output data to the console, then everything works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-09-29
@NeTLeaDeR

Cannot be fixed without using async/promise/ callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question