NodeJS: How do I assign a query result into a variable and use it to populate html table?
I have been doing NodeJS and Express practise since few days. I want to return a query result and assign it on a html table in NodeJS. Here's what I have done:
//My controller
exports.mainpage = function(req,res){
sql = "SELECT * from students";
var qry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(qry);
res.render('layouts/main', qry);
}
Routes:
router.get('/', student_controller.mainpage);
HTML(using handlebars):
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Gender</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
{{#each qry}}
<td>1</td>
<td>{{this.name}}</td>
<td>{{this.gender}}</td>
<td>{{this.dob}}</td>
{{/each}}
</tbody>
</table>
I am getting so many error. I guess my code are wrong. Please help me correct this code.
javascript node.js express handlebars.js
add a comment |
I have been doing NodeJS and Express practise since few days. I want to return a query result and assign it on a html table in NodeJS. Here's what I have done:
//My controller
exports.mainpage = function(req,res){
sql = "SELECT * from students";
var qry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(qry);
res.render('layouts/main', qry);
}
Routes:
router.get('/', student_controller.mainpage);
HTML(using handlebars):
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Gender</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
{{#each qry}}
<td>1</td>
<td>{{this.name}}</td>
<td>{{this.gender}}</td>
<td>{{this.dob}}</td>
{{/each}}
</tbody>
</table>
I am getting so many error. I guess my code are wrong. Please help me correct this code.
javascript node.js express handlebars.js
add a comment |
I have been doing NodeJS and Express practise since few days. I want to return a query result and assign it on a html table in NodeJS. Here's what I have done:
//My controller
exports.mainpage = function(req,res){
sql = "SELECT * from students";
var qry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(qry);
res.render('layouts/main', qry);
}
Routes:
router.get('/', student_controller.mainpage);
HTML(using handlebars):
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Gender</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
{{#each qry}}
<td>1</td>
<td>{{this.name}}</td>
<td>{{this.gender}}</td>
<td>{{this.dob}}</td>
{{/each}}
</tbody>
</table>
I am getting so many error. I guess my code are wrong. Please help me correct this code.
javascript node.js express handlebars.js
I have been doing NodeJS and Express practise since few days. I want to return a query result and assign it on a html table in NodeJS. Here's what I have done:
//My controller
exports.mainpage = function(req,res){
sql = "SELECT * from students";
var qry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(qry);
res.render('layouts/main', qry);
}
Routes:
router.get('/', student_controller.mainpage);
HTML(using handlebars):
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Gender</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
{{#each qry}}
<td>1</td>
<td>{{this.name}}</td>
<td>{{this.gender}}</td>
<td>{{this.dob}}</td>
{{/each}}
</tbody>
</table>
I am getting so many error. I guess my code are wrong. Please help me correct this code.
javascript node.js express handlebars.js
javascript node.js express handlebars.js
edited Nov 21 at 6:16
asked Nov 20 at 17:52
Aabishkar Wagle
12
12
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
What errors are you receiving?
I'll respond to the most obvious problem not knowing all the errors you have:
The most obvious issue is you never define resultqry
in your controller.
You assign the result of your db query to a variable called qry
, but then you send an undefined and undeclared variable resultqry
to your template.
Instead of var qry =
use var resultqry=
var resultqry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(resultqry);
res.render('layouts/main', resultqry);
add a comment |
My assumption is that you are not getting the results in your result object. That is because you need to wait for the results to be returned before you do a render. Try something like following:
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
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%2f53398771%2fnodejs-how-do-i-assign-a-query-result-into-a-variable-and-use-it-to-populate-ht%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What errors are you receiving?
I'll respond to the most obvious problem not knowing all the errors you have:
The most obvious issue is you never define resultqry
in your controller.
You assign the result of your db query to a variable called qry
, but then you send an undefined and undeclared variable resultqry
to your template.
Instead of var qry =
use var resultqry=
var resultqry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(resultqry);
res.render('layouts/main', resultqry);
add a comment |
What errors are you receiving?
I'll respond to the most obvious problem not knowing all the errors you have:
The most obvious issue is you never define resultqry
in your controller.
You assign the result of your db query to a variable called qry
, but then you send an undefined and undeclared variable resultqry
to your template.
Instead of var qry =
use var resultqry=
var resultqry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(resultqry);
res.render('layouts/main', resultqry);
add a comment |
What errors are you receiving?
I'll respond to the most obvious problem not knowing all the errors you have:
The most obvious issue is you never define resultqry
in your controller.
You assign the result of your db query to a variable called qry
, but then you send an undefined and undeclared variable resultqry
to your template.
Instead of var qry =
use var resultqry=
var resultqry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(resultqry);
res.render('layouts/main', resultqry);
What errors are you receiving?
I'll respond to the most obvious problem not knowing all the errors you have:
The most obvious issue is you never define resultqry
in your controller.
You assign the result of your db query to a variable called qry
, but then you send an undefined and undeclared variable resultqry
to your template.
Instead of var qry =
use var resultqry=
var resultqry = db.query(sql,function (err, result) {
if (err) throw err;
return result;
});
console.log(resultqry);
res.render('layouts/main', resultqry);
answered Nov 20 at 18:21
wlh
1,6221722
1,6221722
add a comment |
add a comment |
My assumption is that you are not getting the results in your result object. That is because you need to wait for the results to be returned before you do a render. Try something like following:
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
add a comment |
My assumption is that you are not getting the results in your result object. That is because you need to wait for the results to be returned before you do a render. Try something like following:
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
add a comment |
My assumption is that you are not getting the results in your result object. That is because you need to wait for the results to be returned before you do a render. Try something like following:
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
My assumption is that you are not getting the results in your result object. That is because you need to wait for the results to be returned before you do a render. Try something like following:
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
exports.mainpage = function(req,res){
sql = "SELECT * from students";
db.query(sql)
.then(result => res.render('layouts/main', result);
.catch(error => console.log(error) //put your error handler here
}
answered Nov 20 at 18:56
shmit
44239
44239
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53398771%2fnodejs-how-do-i-assign-a-query-result-into-a-variable-and-use-it-to-populate-ht%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