0

While studying Node, I encountered a problem and asked a question.

View page is using Handlebars.

The controller stores the value as a variable, gets the value from router.get, and sends it to the View page.

However, when used on the View page, the value is not displayed.

Controller Code

exports.boardData = async (req, res, next) => {
    const { id } = req.query;

// If there is an id value
            if(id) {
                db.start.query('SELECT * FROM board WHERE id = ?', [id], async (err, result) => {
                    if(!result) return next();
            
                    result[0].date = moment(result[0].date).format("YYYY년 M월 D일 HH시 mm분");

                    req.board = result[0];

                    checkBoard = result[0].userid;

                    updateCount = result[0].count;

                    if(refreshCheck === false) updateCount = result[0].count + 1;
                   
                    refreshCheck = true;

                    * Here is the problem. *
                    db.start.query('SELECT * FROM comment WHERE boardid = ?', [id], async (err, result) => {
                        if(!result) return next();

                        req.comment = result;
                        req.commentCount = result.length;

                        console.log(req.comment);
                        console.log(req.commentCount);
                    });
    
                    db.start.query('UPDATE board SET count = ? WHERE id = ?', [updateCount, id], async (err, result) => {
                        if(!result) return next();
                    });
                });
            }
}

Here I want to store and send values ​​as req.comment, req.commentCount.

router.get Code

router.get('/boardRead', authController.boardData, (req, res) => {
    res.render('boardRead', {
        user: req.user,
        board: req.board,
        userid: req.userid,
        comment: req.comment,
        commentCount: req.commentCount,
    });
});

View Page Code

<div class="container mt-4">
        <div class="jumbotron" style="border: none;">
            <table class="board-table table table-sm table-hover table-striped text-center" style="border: 1px solid rgba(0, 0, 0, 0.2);">
                <thead class="thead-light">
                    <tr>
                        <td scope="col">comment {{commentCount}}</td>
                    </tr>
                </thead>
                <tbody>
                    {{#if comment}}
                        {{#each comment}}
                           <tr>
                              <td>{{this.comment}}</td>
                              <td>{{this.name}}</td>
                           </tr>
                        {{/each}}
                    {{/if}}
                </tbody>
            </table>
        </div>
    </div>

User or board brings values, but not comments. I did the same way, but I am wondering why I can't bring it to the View page. When I print it with console.log, the value is printed properly.

Hchan2
  • 1
  • https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron The quick fix is to move the UPDATE query code inside the callback function where you successfully log the data. (you seem to be aware of this solution, given that your 2nd SELECT query is inside the 1st SELECT query's callback, just go one callback level deeper again if you will) –  Apr 06 '21 at 11:41
  • @Chris G I tried but it is the same before – Hchan2 Apr 13 '21 at 13:55

0 Answers0