Attempt to multi-render mysql table in node.js...

Asked 2 years ago, Updated 2 years ago, 40 views

Hi, how are you?

I'm trying to load one sql table and one more in the page called read

I just don't know how.

The coding is as follows.

router.get('/read/:idx',function(req,res,next) {

var idx = req.params.idx;

pool.getConnection(function (err, connection) {

    var sql = "select idx, creator_id, title, content, date_format(modidate,'%Y-%m-%d %H:%i:%s') modidate, hit from board where idx = ?";

    connection.query(sql, [idx], function (err, row) {

        if (err) console.error(err);
        console.log("row : " + JSON.stringify(row));

        // View Count Value
        var hit = req.body.hit;
        // Modify view count value
        var sql = 'update board set hit = hit + 1 where idx = ?';
        connection.query(sql, [idx, hit], function (err, result) {
            console.log(result);
        });
        res.render('read', {title: 'test', row: row[0]});

        var sql2 = "select id, m_id, m_name, m_tel, m_date, m_gender, m_email, m_int, m_hello, m_file from users";
        connection.query(sql2, function (err, rows) {
            if (err) console.error("err : " + err);
            console.log("rows : " + JSON.stringify(rows));
        res.render('read', {title: 'test', rows: rows[0]});
        });
        connection.release();
    });
});

});

        res.render('read', {title: 'test', row: row[0]});

I'm going to do re-landing with the read page through this part.

I succeeded in bringing the first table, board, by giving it a row I rendered the second table, users, as rows, but I keep getting errors.

The error content is said to have no rows defined...

I definitely rendered it, but I don't know why there's a problem like this.

Help me ㅠ<

node.js mysql

2022-09-21 17:19

1 Answers

As far as I know, res.render is not more than two times.

After making the code asynchronous, I think we should send row and row when res.render

var idx = req.params.idx;

pool.getConnection(function (err, connection) {

    var sql = "select idx, creator_id, title, content, date_format(modidate,'%Y-%m-%d %H:%i:%s') modidate, hit from board where idx = ?";

    connection.query(sql, [idx], function (err, row) {

        if (err) console.error(err);
        console.log("row : " + JSON.stringify(row));

        var hit = req.body.hit;

        var sql = 'update board set hit = hit + 1 where idx = ?';
        var sql2 = "select id, m_id, m_name, m_tel, m_date, m_gender, m_email, m_int, m_hello, m_file from users";

        connection.query(sql, [idx, hit], function (err, result) {
            console.log(result);
            connection.query(sql2, function (err, rows) {
                if (err) console.error("err : " + err);
                console.log("rows : " + JSON.stringify(rows));
                res.render('read', {title: 'test', row:row[0], rows: rows[0]});
            });
            connection.release();
        });
    });
});

You might have made a mistake because you were in a hurry, but you have to make it asynchronous

And lastly

res.render('read', {title: 'test', row:row[0], rows: rows[0]});

I think you can send it with me like this


2022-09-21 17:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.