How to save the result of a MySql query in a variable using node-mysql

im trying to save the result of the MySql query in a variable using node-mysql model and node.js, so I have this code:

connection.query("select * from ROOMS", function(err, rows){ if(err) { throw err; } else { console.log(rows); } }); 

and the result:

 [ { idRooms: 1, Room_Name: 'dd' }, { idRooms: 2, Room_Name: 'sad' } ] 

so I need to save this result in a variable, so I try like this:

  var someVar = connection.query("select * from ROOMS", function(err, rows){ if(err) { throw err; } else { return rows; } }); console.log(someVar); 

but not working, thanks for any help in advance.

+7
source share
4 answers

Well @Fadi, connection.query is asynchronous, which means your call to console.log(someVar) , someVar is not set yet.

What can you do:

 var someVar = []; connection.query("select * from ROOMS", function(err, rows){ if(err) { throw err; } else { setValue(rows); } }); function setValue(value) { someVar = value; console.log(someVar); } 
+12
source

You cannot do this because network I / O is asynchronous and not blocked in node.js. Therefore, any logic that comes after this, which should only be executed after the request is complete, you must place inside the request callback. If you have a lot of nested asynchronous operations, you can look at using a module such as async to better organize asynchronous tasks.

+2
source

In addition to the answers already provided.

 var **someVar** = connection.query( *sqlQuery*, *callback function( err , row , fields){}* ) console.log(**someVar**); 

This construct will return someVar information for this connection and its SQL query. It will not return values ​​from the query.

The values ​​from the query are in the callback function (err, row, fields)

+2
source

Here is your answer if you want to assign a variable to res.render

 //before define the values var tum_render = []; tum_render.title = "Turan"; tum_render.description = "Turan'ın websitesi"; //left the queries seperatly var rastgeleQuery = "SELECT baslik,hit FROM icerik ORDER BY RAND() LIMIT 1"; var son5Query = "SELECT baslik,hit FROM icerik LIMIT 5"; var query_arr = [rastgeleQuery, son5Query]; var query_name = ['rastgele', 'son5']; //and the functions are //query for db function runQuery(query_arr,sira){ connection.query(query_arr[sira], function(err, rows, fields) { if (err) throw err; var obj = []; obj[query_name[sira]] = rows; sonuclar.push(obj); if (query_arr.length <= sira+1){ sonuc(); }else{ runQuery(query_arr, sira+1); } }); } //the function joins some functions http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array function collect() { var ret = {}; var len = arguments.length; for (var i=0; i<len; i++) { for (p in arguments[i]) { if (arguments[i].hasOwnProperty(p)) { ret[p] = arguments[i][p]; } } } return ret; } //runQuery callback function sonuc(){ for(var x = 0; x<=sonuclar.length-1; x++){ tum_render = collect(tum_render,sonuclar[x]); } console.log(tum_render); res.render('index', tum_render); } 
+1
source

Source: https://habr.com/ru/post/1203624/


All Articles