SQL and Node: How can I run three different query and return the result one by one
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
add a comment |
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
show the queries.
– danblack
Nov 27 '18 at 5:31
add a comment |
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
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
mysql node.js
asked Nov 27 '18 at 5:18
varun chauhanvarun chauhan
3910
3910
show the queries.
– danblack
Nov 27 '18 at 5:31
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);
}
});
});
}
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 noteasync
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
});
});
}
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 noteasync
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
add a comment |
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);
}
});
});
}
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 noteasync
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
add a comment |
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);
}
});
});
}
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);
}
});
});
}
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 noteasync
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
add a comment |
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 noteasync
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
show the queries.
– danblack
Nov 27 '18 at 5:31