Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

node.js - Display count value in the mysql database table using handlebars in Nodejs project

I have a webpage that displays number of applications in certain grades. For example, number of applications in grade 6, grade 7 and grade 8. The functions that I use for grade 6 and grade 7 are as below:

function getGrade6Applicants(req, res, next) {
connection.query('SELECT COUNT(*) AS grade_6 FROM applications WHERE grade="Grade 6" ', function (err, rows, fields) {
    if (err) {
        return next(err);
    };
    req._applications = rows;
    return next();
});}

function getGrade7Applicants(req, res, next) {
connection.query('SELECT COUNT(*) AS grade_7 FROM applications WHERE grade="Grade 7" ', function (err, rows, fields) {
    if (err) {
        return next(err);
    };
    req._applications = rows;
    return next();
});}

Then I use this function to my GET request as below. If it's just for grade_6, it works fine. The name of the mysql table is "applications".

/* GET dashboard page */
router.get('/dashboard', getGrade6Applicants, function (req, res, next) {
   res.render('admission/dashboard', {
       'applications': req._applications
   });
})

This gives me the number of applications in grade6 in my applications table in mysql database.

I use this in my dashboard.handlebars page as {{grade_6}} using appropriate {{#if}} and {{#each}} built in helpers.

The problem is whenever I wanted to add second function to display number of applications in grade 7, what I have is only grade 7 applications. Grade 6 is not shown. Here is the GET request that I use for multiple values:

/* GET dashboard page */
router.get('/dashboard', getGrade6Applicants, getGrade7Applicants, function (req, res, next) {
   res.render('admission/dashboard', {
       'applications': req._applications
   });
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my solution that works:

my router js file:

router.get('/dashboard', getGrade7Applicants, getGrade8Applicants, function (req, res, next) {
res.render('admission/dashboard', {
    applications: {
            'grade7': req._applications7[0],
            'grade8': req._applications8[0],
    }

});
});

And here is the html template:

 {{#if applications}}
     {{#if applications.grade7}}
         {{applications.grade7.grade7}}
      {{/if}}
     .......
   {{#if applications.grade8}}
         {{applications.grade8.grade8}}
      {{/if}}
{{/if}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...