Override issue when display mysql data in Nodejs

100 views
Skip to first unread message

Fatih Karatay

unread,
Jan 5, 2017, 4:04:06 PM1/5/17
to nodejs
I have a web page 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
   });
})







Alex Wells

unread,
Jan 10, 2017, 8:35:49 AM1/10/17
to nodejs
Just so you know, it's a bit weird to set a 'private' property on an object you don't own:

    req._applications = rows;

although this is just by convention, if you were to log a bug with something I had developed where you'd made use of the above, I would probably ask you to change this first. If you want to set something on the req object, most people set a 'data' variable like so:

    req.data = {
        grade6Rows: rows;
    };

In the above is also the solution - you are setting the same property both times ('_applications') so it's getting overwritten by subsequent callbacks. You need to place multiple properties in the 'req' object for each resultset.

You would do that in each callback with something like this;

    req.data = req.data || {};
    req.data['grade6Rows'] = rows;

Remember to decode the JSON on the client, and you'll have an object you can iterate.

Fatih Karatay

unread,
Jan 10, 2017, 8:54:28 PM1/10/17
to nodejs
When I try your solution, this time nothing is displayed. Am I missing anything? 

Alex Wells

unread,
Jan 11, 2017, 12:10:24 PM1/11/17
to nodejs
You're probably not decoding the JSON in the client and then choosing the right variables. I haven't got your Template so I can't help you fix that, but you'll need to reference the 'data.grade6Rows' and 'data.grade7Rows' objects you created, rather than the '_applications.grade6Rows' you were referencing previously.

Fatih Karatay

unread,
Jan 24, 2017, 2:26:20 PM1/24/17
to nod...@googlegroups.com
Here is the router js file that I use :

//connection variables
var connection = mysql.createConnection({
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: '12345*',
    dateStrings: true,
    database: 'database'

});

connection.connect();


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

function getGrade8Applicants(req, res, next) {
    connection.query('SELECT COUNT(*) AS grade8 FROM applications WHERE grade="Grade 8" ', function (err, rows, fields) {
        if (err) {
            return next(err);
        };
        req._applications8 = rows;
        return next();
    });
}



/* GET dashboard page */

router.get('/dashboard', getGrade7Applicants, getGrade8Applicants, function (req, res, next) {
    res.render('admission/dashboard', {
        'applications': req._applications7,
        'applications': req._applications8,

    });
});

And here is my html template. I use handlebars. 

<div class="alert alert-success" role="alert">
      <p style="font-size:75px; text-align:center;">{{grade7}}</p>
       <p style="text-align:center;"><strong>7th GRADE Applications <strong> </p>
</div>

<div class="alert alert-success" role="alert">
      <p style="font-size:75px; text-align:center;">{{grade8}}</p>
       <p style="text-align:center;"><strong>8th GRADE Applications <strong> </p>
</div>

I know there is override in here. What would be best way to display those count values from the same table in the same page?

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/75ac96b4-05d4-4948-b2ec-699a86001c8b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Fatih Karatay
Dove Science Academy Tulsa

Muhammad Wasim

unread,
Jan 25, 2017, 2:18:50 PM1/25/17
to nod...@googlegroups.com
Set it as following

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

    });
});

And here is my html template. I use handlebars. 

<div class="alert alert-success" role="alert">
      <p style="font-size:75px; text-align:center;">{{grade7}}</p>
       <p style="text-align:center;"><strong>7th GRADE Applications <strong> </p>
</div>

<div class="alert alert-success" role="alert">
      <p style="font-size:75px; text-align:center;">{{grade8}}</p>
       <p style="text-align:center;"><strong>8th GRADE Applications <strong> </p>
</div>

Muhammad Wasim.

Fatih Karatay

unread,
Jan 26, 2017, 12:16:03 AM1/26/17
to nodejs
When I do this, nothing is displayed. That drives me crazy. Here is my html teample completely: 

<div class="container">
    <div class="row">
        <h2>Number of Applications</h2>
        {{#if applications}}
        {{#each applications}}
        <div class="col-lg-2">
            <div class="alert alert-success" role="alert">
                <p style="font-size:75px; text-align:center;">{{grade7}}</p>
                <p style="text-align:center;"><strong>7th GRADE Applications <strong> </p>
            </div>
        </div>
        <div class="col-lg-2">
            <div class="alert alert-warning" role="alert">
                <p style="font-size:75px; text-align:center;">{{grade8}}</p>
                <p style="text-align:center;"><strong>8th GRADE Applications <strong> </p>
            </div>
        </div>
        {{/each}}
        {{else}}
            <p>No Applications</p>
        {{/if}}
        </div>
    </div>
</div>
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.
--
Fatih Karatay
Dove Science Academy Tulsa

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

To post to this group, send email to nod...@googlegroups.com.

Alex Wells

unread,
Jan 26, 2017, 2:08:03 PM1/26/17
to nodejs
OK, so take a look at your template. Why are you iterating through the 'applications' variable? 

After Muhammad's (good) advice you have changed the one variable called 'applications' (it was being overwritten, because you wrote it twice) with two variables called 'grade7' and 'grade8'.

All of that would be fine, but in your template you don't display anything if there's no 'applications' variable.

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

The object marked in red is the one available to the context of the page, as you can see there's no 'applications' to iterate. So, on to solutions. You could remove the 'if' and 'each' portions of your template, so that you simply print the contents of your object into the template, but that requires a fairly static template. Or you could put the variables you want to display within an 'applications' object, like this:

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

For your next bit, consider moving the labels to the object:

router.get('/dashboard', getGrade7Applicants, getGrade8Applicants, function (req, res, next) {
    res.render('admission/dashboard', {
        grades: [{
                'label': '7th GRADE Applications',
                'count': req._applications7
        }, {
                'label': '8th GRADE Applications',
                'count': req._applications8
        }]
    });
});

and then iterate through the objects and display them as required.

Fatih Karatay

unread,
Jan 26, 2017, 4:53:48 PM1/26/17
to nodejs
Thank you, Alex and Muhammad, for your help. 
I know this took a long time for us. I tried pretty much all of your suggestions. All I see is [object Object]
I don't see where my problem is. Anyway, thank you again for your time to deal with this. 
Reply all
Reply to author
Forward
0 new messages