node.js get result from mysql query - mysql

why I cannot get results?
var sql_data = connection.query(sql, function(error, results, fields) {
if(error) {
console.log(error);
return;
}
var rows = JSON.parse(JSON.stringify(results[0]));
console.log(rows);
});
console.log(rows);
fiirst console.log is ok, display object,
but second says:
ReferenceError: rows is not defined
what is wrong?..

You shouldn't assign asynchronous function to a variable just like you do in first line of your code. You just call it and perform operations on the result with use of callback methods (which in this case is function(error, results, fields). Your code should look like below
connection.query(sql, function(error, results, fields) {
if(error) {
console.log(error);
return;
}
var rows = JSON.parse(JSON.stringify(results[0]));
// here you can access rows
console.log(rows);
});
// here it will be undefined
console.log(rows);
The rows variable in second console.log will be undefined because it was defined in different scope. Even if you would do var rows above the connection.query, it would still be undefined, because you assign it's value inside asynchronous function's callback. You need to read more about this kind of operations.

You should use then Promise if you want to get the query result. I prefere it to be onest. The Promise runs the command async.
function getDomain() {
return result = await dbQuery('SELECT name FROM virtual_domains ORDER BY id;');
}
// * Important promise function
function dbQuery(databaseQuery) {
return new Promise(data => {
db.query(databaseQuery, function (error, result) { // change db->connection for your code
if (error) {
console.log(error);
throw error;
}
try {
console.log(result);
data(result);
} catch (error) {
data({});
throw error;
}
});
});
}

Related

Calling a function which calls Async mysql

I want to call a function that executes a query using npm-mysql .query function. The problem is that .query is asynchronous so I get a returned value = undefined and after that the mysql.query finishes the execution.
I 've tried to use promises but I couldn't synchronize the return value with the mysql.query result.
I don't want to use sync-mysql.
I want it to be in a wrapper function as shown.
function mysql_select(query)
{
var json_result
mysql_connnection.query(query, function (err, result)
{
if (err) throw err
json_result = JSON.stringify(result)
})
return json_result
}
For example i want to call this function like this:
console.log(mysql_select("SELECT * FROM table"))
and dont get the undefined result
I have checked the query , it returns the data correctly but after the function returns the json_result.
You might want to have a look into Promises:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
function mysql_select(query)
{
return new Promise(function(resolve, reject) {
mysql_connnection.query(query, function (err, result)
{
if (err) {
reject(err);
return;
}
resolve(JSON.stringify(result));
})
})
}
;(async function() {
console.log(await mysql_select('SELECT * FROM table'));
})();

NodeJS and SQL with Async

I'm trying to use a SQL query with NodeJs but the async part really mess me up. When i print the return it gets "undefined". How can i sync this?
function SQL_test(blos) {
DB.query("Select profesor_id from materiador where materia_id = '"+blos+"';",function (err, results, fields) {
return results;
});
}
console.log(SQL_test(1));
Thanks!
So your answer is currently a promise. You'll have to read about Async and Await to do more synchronous JS.
Most of the JS for NodeJS is currently async. Below is a rewritten version of your example properly utilizing the callback method for your DB.
function callback (err, results, fields) {
if (err) {
console.log(err);
return err;
}
console.log(results, fields);
return results;
};
function SQL_test(blos) {
DB
.query("Select profesor_id from materiador where materia_id = '"+blos+"';", callback);
}
SQL_test(1);
To do the same thing synchronously you have to still have an outer level promise, otherwise Async and Await won't work how you want it to. There's no true synchronous way to handle this is javascript because it executes without waiting for the response.
function sqlTest(blos) {
return new Promise((resolve, reject) => {
DB.query(
`Select profesor_id from materiador where materia_id = '${blos}';`,
(err, results) => {
if (err) return reject(err)
resolve(results)
}
);
}
)
sqlTest(1)
.then(console.log)
.catch(console.error)

Converting csv to json object returns undefined

I've found several examples on S/O and otherwise, but they don't seem to be helping me.
I'm creating a private module in node that takes in a csv and converts it into a json object. It is properly outputting the correct value onto the command line, but the object itself is undefined.
exports.csvToJSON = function(file, callback) {
converter.fromFile(file, function(err, result) {
if (err) {
console.log('error: ', error)
} else {
return result;
callback(err, result);
}
});
console.log(callback)
}
I'm currently using the csvtojson module and have tried other similar packages. Articles I've referenced:
Function Returns undefined object
Why does this callback return undefined?
Why is function return value undefined when returned in a loop? (although not entirely relevant as this is not a loop function)
Callback returning undefined
I'm unsure if I'm just not understanding the callback correctly, but even if I console.log(result.type), it returns back undefined with or without the callback. I've also tried defining the callback like so:
exports.csvToJSON = function(file, callback) {
csvtojson().fromFile(file, function(err, result) {
if (err) {
console.log('error: ', error)
}
return result;
callback(result);
});
}
Here's an example of the console output:
Mirandas-MacBook-Pro:zendesktool mirandashort$ node ./controllers/update.js
[ { ticket:
{ ids: '3280805',
requester_email: 'miranda#barkbox.com',
status: 'pending',
comment: [Object],
subject: 'sup dog',
custom_fields: [Object],
add_tags: 'update_via_csv, dogs_are_cool, sup' } } ] undefined
Right now, since my other functions are dependent on this working, I'm only calling it in the file with exports.csvToJSON('update2.csv') where update2.csv is an actual file. Eventually this will be called inside another function in which I will be utilizing async, but I need this to work first. Additionally, that output seems to be linked to console.log(err) when called by the second code block example, which I'm not to sure why.
Or if there's a way to do this altogether without csvtojson, that's fine too. The only requirement be that a file in csv format can be returned as an object array.
Got it. I just used waterfall to put the two individual modeles together:
exports.csvToJSON = function(file, callback) {
csvtojson().fromFile(file, function(err, result) {
if (err) {
console.log(err);
} else {
callback(null, result[0]);
}
});
}
exports.zdUpdateMany = function(data, callback) {
credentials.config.tickets.updateMany(3280805, data, function(err, result) {
if (err) {
console.log(err);
} else {
callback(false, result);
}
});
}
// function to update tickets
exports.processUpdate = function(file, callback) {
async.waterfall([
async.apply(exports.csvToJSON, file),
exports.zdUpdateMany
], function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
callback(err, result);
});
}

nodejs- unable to return result to controller function

From my Model, I fetch some articles from a MySQL database for a user.
Model
var mysql = require('mysql');
var db = mysql.createPool({
host: 'localhost',
user: 'sampleUser',
password: '',
database: 'sampleDB'
});
fetchArticles: function (user, callback) {
var params = [user.userId];
var query = `SELECT * FROM articles WHERE userId = ? LOCK IN SHARE MODE`;
db.getConnection(function (err, connection) {
if (err) {
throw err;
}
connection.beginTransaction(function (err) {
if (err) {
throw err;
}
return connection.query(query, params, function (err, result) {
if (err) {
connection.rollback(function () {
throw err;
});
}
//console.log(result);
});
});
});
}
This is working and the function fetches the result needed. But it's not returning the result to the controller function (I am returning it but I'm not able to fetch it in the controller function. I guess, I did something wrong here).
When I did console.log(result) this is what I got.
[ RowDataPacket {
status: 'New',
article_code: 13362,
created_date: 2017-10-22T00:30:00.000Z,
type: 'ebook'} ]
My controller function looks like this:
var Articles = require('../models/Articles');
exports.getArticle = function (req, res) {
var articleId = req.body.articleId;
var article = {
userId: userId
};
Articles.fetchArticles(article, function (err, rows) {
if (err) {
res.json({ success: false, message: 'no data found' });
}
else {
res.json({ success: true, articles: rows });
}
});
};
Can anyone help me figure out what mistakes I made here?
I'm pretty new to nodejs. Thanks!
The simple answer is that you're not calling the callback function, anywhere.
Here's the adjusted code:
fetchArticles: function (user, callback) {
var params = [user.userId];
var query = `SELECT * FROM articles WHERE userId = ? LOCK IN SHARE MODE`;
db.getConnection(function (err, connection) {
if (err) {
// An error. Ensure `callback` gets called with the error argument.
return callback(err);
}
connection.beginTransaction(function (err) {
if (err) {
// An error. Ensure `callback` gets called with the error argument.
return callback(err);
}
return connection.query(query, params, function (err, result) {
if (err) {
// An error.
// Rollback
connection.rollback(function () {
// Once the rollback finished, ensure `callback` gets called
// with the error argument.
return callback(err);
});
} else {
// Query success. Call `callback` with results and `null` for error.
//console.log(result);
return callback(null, result);
}
});
});
});
}
There's no point in throwing errors inside the callbacks on the connection methods, since these functions are async.
Ensure you pass the error to the callback instead, and stop execution (using the return statement).
One more thing, without knowing the full requirements of this:
I'm not sure you need transactions for just fetching data from the database, without modifying it; so you can just do the query() and skip on using any beginTransaction(), rollback() and commit() calls.

nodejs-async: return mysql query result in for loop

I have three operations to do one after another
1.Fetch some rows from db
2.Another mysql query in forloop for getting some data and store in variable
3.Display the data
For that i am using async waterfall method.
async.waterfall([
function(callback){
//first sql and pass the result to second function
collection.getAllCollections(function (status,error,result) {
callback(null,result);
});
},
//running another query in loop with result with previous
function(result, callback){
for(var i=0;i<result.length;i++){
collection.getImages(result[i].id,function (status,error,user) {
//append the query result to old result
result[i]['users'] = user;
});
}
callback(null,result);
}
], function (err, result) {
console.log("result",result);
});
But the problem final result does not contains the user results because the second query(query in for loop is asynchronous)
You realised the problem at hand. Your callback basically has to wait for the for loop to end.
For example like this:
async.waterfall([
function(next){
//first sql and pass the result to second function
collection.getAllCollections(function (status,error,result) {
next(null, result);
});
},
function(result, next){
var calls = [];
//putting every call in an array
result.forEach(function(resultObject){
calls.push(function(callback) {
collection.getImages(resultObject.id, function (status, error, user) {
resultObject['users'] = user;
callback(null, resultObject);
});
}
)});
//performing calls async parallel to each other
async.parallel(calls, function(err, results) {
//executed as soon as all calls are finished
if (err) {
next(err, null);
} else {
next(null, results);
}
});
}
], function (err, result) {
console.log("result",result);
});
Documentation: http://caolan.github.io/async/docs.html#parallel