#problem using sql consult(javascript)

20 messages · Page 1 of 1 (latest)

weak plinth
#

my code no return a result of SELECT, help please

#
const result = sql.query('SELECT * FROM users', function(err, result) {
    if (err) throw err;

    return result[0].email;
})
fading scaffold
#

Is there an email set for the first row of the result?
What do you get with:```js
const sqlresult = sql.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
return result;
});

weak plinth
#

yes

#

if i try use

console.log(sqlresult) he return this:

fading scaffold
#

or..```js
const sqlresult = sql.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
console.log(result[0]);
return result[0].email;
});

weak plinth
#

not work, i try use let on top a cod

#

but, no results

#
let resultado

const sqlresult = sql.query('SELECT * FROM users', function(err, result) {
    if (err) throw err;
    resultado = result
});
weak plinth
fading scaffold
weak plinth
#

lfmao, this is a caos

#

i using nodejs to execute all codes

#

if i use mongodb is more ez than use mySQL

#

somepersons say i need use callback, i need study this, i never use this

fading scaffold
#

Yeah you cannot use the result until it's returned from the server and we never know how long that takes.

weak plinth
#

yeah, i learning to callback tomorrow, i stay tired now, thank you for your help bro

#

have a good night^

fading scaffold
#

Cheers!
FWIW: the way you're writing this sets your function to be the callback code. You could even look at it like this: ```js
var sqlrow = '';
function SQcallBack(err, result) {
if (err) throw err;
sqlrow = result[0].email;
// do the next things
console.log(sqlrow);
}
const sqlresult = sql.query('SELECT * FROM users', SQcallBack(err, result));