#problem using sql consult(javascript)
20 messages · Page 1 of 1 (latest)
const result = sql.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
return result[0].email;
})
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;
});
or..```js
const sqlresult = sql.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
console.log(result[0]);
return result[0].email;
});
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
});
if i use this, console.log(result[0]) work, but if i use
console.log(sqlresult), return is soooo different
Yeah the sqlresult is the query, not just the result? You could just set a global var?```js
var sqlrow = '';
const sqlresult = sql.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
sqlrow = result[0].email;
// do the next thing
console.log(sqlrow);
});
i try soo
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
Yeah you cannot use the result until it's returned from the server and we never know how long that takes.
yeah, i learning to callback tomorrow, i stay tired now, thank you for your help bro
have a good night^
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));