NodeJS: How do I assign a query result into a variable and use it to populate html table?












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 6:16







      Aabishkar Wagle

















      asked Nov 20 '18 at 17:52









      Aabishkar WagleAabishkar Wagle

      12




      12
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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);





          share|improve this answer































            0














            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


            }








            share|improve this answer























              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%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









              0














              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);





              share|improve this answer




























                0














                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);





                share|improve this answer


























                  0












                  0








                  0







                  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);





                  share|improve this answer













                  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);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 18:21









                  wlhwlh

                  1,6451722




                  1,6451722

























                      0














                      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


                      }








                      share|improve this answer




























                        0














                        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


                        }








                        share|improve this answer


























                          0












                          0








                          0







                          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


                          }








                          share|improve this answer













                          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


                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 '18 at 18:56









                          shmitshmit

                          45739




                          45739






























                              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%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





















































                              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

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              RAC Tourist Trophy