Good day. I work with node js. I have a function that returns the values ​​selected from the database. Code on the server

function selectingManufacturer(callback) { var manufacturerdbArr = []; pool.pool.query('select name from manufacturer', function (err, results) { if (err) { callback(err,null); }else { for (var i = 0; i < results.length; i++) { manufacturerdbArr.push(results[i].name); } callback(err,manufacturerdbArr); } }); } selectingManufacturer(function(err,manufacturerdbArr){ if(err){ console.log("ERROR : ",err); }else{ .... }); 

But besides the data from one table, I need to select them from another. Is it possible to do this and that the data from both tables would be available in one function? I understand approximately how it should work, but in practice it does not work out

  • Well, execute both requests asynchronously, wait for a response from each of the requests and operate on results of two or more requests. Well, or you can do so - request the 1st request => to success 1 to make a call to the 2nd request => success 2 request. Bottom line: you have a result of 1 and a result of success for the 2nd request. Promises are your main “weapon” - alexoander

1 answer 1

In the forehead, it will look like this -

 запрос_в_первую_таблицу(function(err,данные_1){ запрос_во_вторую_таблицу(function(err,данные_2){ //Теперь у вас есть и данные_1 и данные_2 ваша_функция(данные_1,данные_2); }); });