将数据从mysql填充到ejs

时间:2017-03-01 20:03:25

标签: html mysql node.js ejs

Link to github https://github.com/AEkman/Quiz

我试图从mysql数据库获取数据并使用EJS打印到html表。

这是我在database.js上的函数调用:

    this.getUsers = function (res) {
    connection.acquire(function (err, con) {
        con.query('SELECT * FROM user', function (err, rows, result) {
            con.release();
            if(err) {
                console.log(err);
            } else {
                obj = JSON.parse(JSON.stringify(rows));
                console.log(obj);
            }
        });
    });
};
};

这是我在app.js中的路线

app.get('/settings', function(req, res) {
var obj = databaseFunctions.getUsers(res);
res.render('settings', {
    obj:obj,
    title: 'Settings',
    classname: 'settings'
});
});

这是我的EJS代码:

<% print.forEach(function(users) { %>
<tr>
    <td><%= users.mail %></td>
    <td><%= users.name %></td>
    <td><%= users.password %></td>
    <td><%= users.groups %></td>
    <td><%= users.accountLevel %></td>
</tr>
<% }); %>

如果我控制台.log(obj)我得到:

[ { mail: 'info@andreasekman.com',
name: 'test',
password: '1234',
groups: 'java2',
accountLevel: 'User' },
{ mail: 'xtironman@hotmail.com',
name: 'ace',
password: 'ace',
groups: 'java2',
accountLevel: 'User' } ]

当我尝试运行/设置时,我得到以下内容:

    TypeError: C:\Users\Andreas\Documents\GitHub\Quiz\views\settings.ejs:15
   13|         <h1>Settings</h1> 
   14|         <% include partials/template/createuser.ejs %> 
>> 15|         <% include partials/template/viewusers.ejs %> 
   16|     </div><!-- row --> 
   17|     <div class="content"></div> 
   18|     <% include partials/template/footer.ejs %> 

esc is not a function
   at rethrow (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:285:18)
   at eval (eval at compile (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:524:12), <anonymous>:85:9)
   at eval (eval at compile (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:524:12), <anonymous>:87:10)
   at returnedFn (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:555:17)
   at tryHandleCache (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:203:34)
   at View.exports.renderFile [as engine] (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\ejs\lib\ejs.js:412:10)
   at View.render (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\express\lib\view.js:126:8)
   at tryRender (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\express\lib\application.js:639:10)
   at EventEmitter.render (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\express\lib\application.js:591:3)
   at ServerResponse.render (C:\Users\Andreas\Documents\GitHub\Quiz\node_modules\express\lib\response.js:960:7)

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我发现问题至少,在获取数据并将其保存到变量后,数据在运行if语句后消失。所以我需要弄清楚如何传递变量。

有什么建议吗?

答案 1 :(得分:0)

我把查询放在路线中,不是很漂亮但是做了工作!

def negate(function):
    def new_function(*args, **kwargs):
       return not function(*args, **kwargs)
return new_function


def is_even(x):
    return x % 2 == 0

print is_even(1)
print is_even(2)

is_odd = negate(is_even)
print is_odd(1)
print is_odd(2)