SQL and Node: How can I run three different query and return the result one by one












1















I have total of 3 SQL query. I have done my connection with database through node.js. How can I write a function that helps me to run all the 3 query one by one and return the result as json to angular front end.



This is the code that I have. Is there any better way to make a call to the query as return then one by one. I am using the post method for the api.



    var sqlquery=" " 
var sqlsecond=" "
var sqlthird=" "

mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result);

}
selectquery2(sqlsecond)
selectquery3(sqlthird)

})

})
function selectquery2(sqlquery1){
mysqlConnection.query(sqlquery1,(err,result1)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result1);

}
})
}
function selectquery3(sqlquery2){
mysqlConnection.query(sqlquery2,(err,result2)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result2);

}
})
}









share|improve this question























  • show the queries.

    – danblack
    Nov 27 '18 at 5:31
















1















I have total of 3 SQL query. I have done my connection with database through node.js. How can I write a function that helps me to run all the 3 query one by one and return the result as json to angular front end.



This is the code that I have. Is there any better way to make a call to the query as return then one by one. I am using the post method for the api.



    var sqlquery=" " 
var sqlsecond=" "
var sqlthird=" "

mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result);

}
selectquery2(sqlsecond)
selectquery3(sqlthird)

})

})
function selectquery2(sqlquery1){
mysqlConnection.query(sqlquery1,(err,result1)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result1);

}
})
}
function selectquery3(sqlquery2){
mysqlConnection.query(sqlquery2,(err,result2)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result2);

}
})
}









share|improve this question























  • show the queries.

    – danblack
    Nov 27 '18 at 5:31














1












1








1








I have total of 3 SQL query. I have done my connection with database through node.js. How can I write a function that helps me to run all the 3 query one by one and return the result as json to angular front end.



This is the code that I have. Is there any better way to make a call to the query as return then one by one. I am using the post method for the api.



    var sqlquery=" " 
var sqlsecond=" "
var sqlthird=" "

mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result);

}
selectquery2(sqlsecond)
selectquery3(sqlthird)

})

})
function selectquery2(sqlquery1){
mysqlConnection.query(sqlquery1,(err,result1)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result1);

}
})
}
function selectquery3(sqlquery2){
mysqlConnection.query(sqlquery2,(err,result2)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result2);

}
})
}









share|improve this question














I have total of 3 SQL query. I have done my connection with database through node.js. How can I write a function that helps me to run all the 3 query one by one and return the result as json to angular front end.



This is the code that I have. Is there any better way to make a call to the query as return then one by one. I am using the post method for the api.



    var sqlquery=" " 
var sqlsecond=" "
var sqlthird=" "

mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result);

}
selectquery2(sqlsecond)
selectquery3(sqlthird)

})

})
function selectquery2(sqlquery1){
mysqlConnection.query(sqlquery1,(err,result1)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result1);

}
})
}
function selectquery3(sqlquery2){
mysqlConnection.query(sqlquery2,(err,result2)=>{
if(err){
console.log("Error"+err);
}
else{
return response.json(result2);

}
})
}






mysql node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 5:18









varun chauhanvarun chauhan

3910




3910













  • show the queries.

    – danblack
    Nov 27 '18 at 5:31



















  • show the queries.

    – danblack
    Nov 27 '18 at 5:31

















show the queries.

– danblack
Nov 27 '18 at 5:31





show the queries.

– danblack
Nov 27 '18 at 5:31












1 Answer
1






active

oldest

votes


















0














You can use Promise and ready all 3 results then return all of them together in json format. For example



router.get('/', async function(req, response){
try{
var sqlquery=" "
var sqlsecond=" "
var sqlthird=" "

let result1 = await selectquery(sqlquery)

let result2 = await selectquery(sqlsecond)

let result3 = await selectquery(sqlthird)

return response.json({result1:result1, result2:result2, result3:result3});

}
catch(err){
response.status(500).end();
}
})


async function selectquery(sqlquery){
return new Promise((resolve, reject) => {
mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
reject(err);
}
else{
resolve(result);
}
});
});
}





share|improve this answer
























  • i will try this approach

    – varun chauhan
    Nov 27 '18 at 5:43











  • when i run the code an error pops up saying that await is only valid in async function.

    – varun chauhan
    Nov 27 '18 at 5:53











  • Please note async on first line

    – Milad Aghamohammadi
    Nov 27 '18 at 6:40











  • thank you, it worked for me

    – varun chauhan
    Nov 27 '18 at 8:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493192%2fsql-and-node-how-can-i-run-three-different-query-and-return-the-result-one-by-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use Promise and ready all 3 results then return all of them together in json format. For example



router.get('/', async function(req, response){
try{
var sqlquery=" "
var sqlsecond=" "
var sqlthird=" "

let result1 = await selectquery(sqlquery)

let result2 = await selectquery(sqlsecond)

let result3 = await selectquery(sqlthird)

return response.json({result1:result1, result2:result2, result3:result3});

}
catch(err){
response.status(500).end();
}
})


async function selectquery(sqlquery){
return new Promise((resolve, reject) => {
mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
reject(err);
}
else{
resolve(result);
}
});
});
}





share|improve this answer
























  • i will try this approach

    – varun chauhan
    Nov 27 '18 at 5:43











  • when i run the code an error pops up saying that await is only valid in async function.

    – varun chauhan
    Nov 27 '18 at 5:53











  • Please note async on first line

    – Milad Aghamohammadi
    Nov 27 '18 at 6:40











  • thank you, it worked for me

    – varun chauhan
    Nov 27 '18 at 8:57
















0














You can use Promise and ready all 3 results then return all of them together in json format. For example



router.get('/', async function(req, response){
try{
var sqlquery=" "
var sqlsecond=" "
var sqlthird=" "

let result1 = await selectquery(sqlquery)

let result2 = await selectquery(sqlsecond)

let result3 = await selectquery(sqlthird)

return response.json({result1:result1, result2:result2, result3:result3});

}
catch(err){
response.status(500).end();
}
})


async function selectquery(sqlquery){
return new Promise((resolve, reject) => {
mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
reject(err);
}
else{
resolve(result);
}
});
});
}





share|improve this answer
























  • i will try this approach

    – varun chauhan
    Nov 27 '18 at 5:43











  • when i run the code an error pops up saying that await is only valid in async function.

    – varun chauhan
    Nov 27 '18 at 5:53











  • Please note async on first line

    – Milad Aghamohammadi
    Nov 27 '18 at 6:40











  • thank you, it worked for me

    – varun chauhan
    Nov 27 '18 at 8:57














0












0








0







You can use Promise and ready all 3 results then return all of them together in json format. For example



router.get('/', async function(req, response){
try{
var sqlquery=" "
var sqlsecond=" "
var sqlthird=" "

let result1 = await selectquery(sqlquery)

let result2 = await selectquery(sqlsecond)

let result3 = await selectquery(sqlthird)

return response.json({result1:result1, result2:result2, result3:result3});

}
catch(err){
response.status(500).end();
}
})


async function selectquery(sqlquery){
return new Promise((resolve, reject) => {
mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
reject(err);
}
else{
resolve(result);
}
});
});
}





share|improve this answer













You can use Promise and ready all 3 results then return all of them together in json format. For example



router.get('/', async function(req, response){
try{
var sqlquery=" "
var sqlsecond=" "
var sqlthird=" "

let result1 = await selectquery(sqlquery)

let result2 = await selectquery(sqlsecond)

let result3 = await selectquery(sqlthird)

return response.json({result1:result1, result2:result2, result3:result3});

}
catch(err){
response.status(500).end();
}
})


async function selectquery(sqlquery){
return new Promise((resolve, reject) => {
mysqlConnection.query(sqlquery,(err,result)=>{
if(err){
reject(err);
}
else{
resolve(result);
}
});
});
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 5:42









Milad AghamohammadiMilad Aghamohammadi

851516




851516













  • i will try this approach

    – varun chauhan
    Nov 27 '18 at 5:43











  • when i run the code an error pops up saying that await is only valid in async function.

    – varun chauhan
    Nov 27 '18 at 5:53











  • Please note async on first line

    – Milad Aghamohammadi
    Nov 27 '18 at 6:40











  • thank you, it worked for me

    – varun chauhan
    Nov 27 '18 at 8:57



















  • i will try this approach

    – varun chauhan
    Nov 27 '18 at 5:43











  • when i run the code an error pops up saying that await is only valid in async function.

    – varun chauhan
    Nov 27 '18 at 5:53











  • Please note async on first line

    – Milad Aghamohammadi
    Nov 27 '18 at 6:40











  • thank you, it worked for me

    – varun chauhan
    Nov 27 '18 at 8:57

















i will try this approach

– varun chauhan
Nov 27 '18 at 5:43





i will try this approach

– varun chauhan
Nov 27 '18 at 5:43













when i run the code an error pops up saying that await is only valid in async function.

– varun chauhan
Nov 27 '18 at 5:53





when i run the code an error pops up saying that await is only valid in async function.

– varun chauhan
Nov 27 '18 at 5:53













Please note async on first line

– Milad Aghamohammadi
Nov 27 '18 at 6:40





Please note async on first line

– Milad Aghamohammadi
Nov 27 '18 at 6:40













thank you, it worked for me

– varun chauhan
Nov 27 '18 at 8:57





thank you, it worked for me

– varun chauhan
Nov 27 '18 at 8:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493192%2fsql-and-node-how-can-i-run-three-different-query-and-return-the-result-one-by-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)