Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Holen Sie sich MySQL-Daten in node.js (Express) und drucken Sie mit EJS

Beim Rendern können Sie eine JSON-Variable mit den abgerufenen Daten zurückgeben und in der Ansicht anzeigen. res.render kann die letzte Anweisung auf diese Weise sein. Einige davon:

var obj = {};
router.get('/data', function(req, res){

    connection.query('SELECT * FROM users', function(err, result) {

        if(err){
            throw err;
        } else {
            obj = {print: result};
            res.render('print', obj);                
        }
    });

});

<body>
    <table id="table" >  
        <thead>  
            <tr>  
                <th>Username</th>  
                <th>Password</th>  
            </tr>  
        </thead>  
         <tbody>  
         <% print.forEach(function (user) { %>
            <tr>  
                <td><%= user.username %></td>  
                <td><%= user.password %></td>
            </tr>                                   
         <% }) %>
         </tbody>
    </table>   
</body>

</html>