使用Node.js和Handlebars显示数据库查询的内容

时间:2018-03-28 17:49:10

标签: javascript node.js handlebars.js

我正在从一个页面路由到另一个页面,并且我试图在用户display.js的下一页上显示数据库查询的内容。

  install.packages ("data.table")
  library (data.table)

  foo = structure(list(V2 = c("Saturday", "Saturday", "Saturday", "Sunday", 
                       "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Saturday", 
                       "Saturday", "Saturday", "Saturday", "Sunday", "Sunday", "Saturday", 
                       "Saturday", "Sunday"), V3 = c("Evening", "Evening", "Evening", 
                                                     "Evening", "Evening", "Evening", "Evening", "Evening", "Evening", 
                                                     "Night", "Night", "Night", "Night", "Night", "Night", "Night", 
                                                     "Night", "Night"), V4 = c(16.2, 23.4, 29.4, 24.2, 24.2, 24.2, 
                                                                               24.2, 25.4, 26.8, 25.6, 24.4, 24.4, 24.4, 25.2, 25.2, 25.2, 25.2, 
                                                                               25.2), V5 = c(235.84, 235.29, 232.79, 233.89, 233.66, 233.38, 
                                                                                             232.99, 233.21, 232.37, 231.55, 231.19, 231.63, 231.71, 231.23, 
                                                                                             231.23, 231.23, 231.23, 231.23)), .Names = c("V2", "V3", "V4", 
                                                                                                                                          "V5"), row.names = c(NA, -18L), class = "data.frame")

  foo <- as.data.table (foo) #convert to a datatable
  nrow (subset (foo, V2 == "Saturday")) #count how many rows are Saturday
  nrow (subset (foo, V2 == "Sunday")) #count how many rows are Sunday
  nrow (subset (foo, V2 == "Saturday" & V3 == "Evening")) #count how many rows are Saturday Evening
  nrow (subset (foo, V2 == "Saturday" & V3 == "Evening" | V3 == "Night")) #count how many rows are Saturday Evening and Night

}

所以我有数据,但现在我需要显示它。我尝试使用把手将tempdisplay.html文件中res.render()发送的数据对象转换为使用

的字符串
exports.list = (req, res) => {
console.log("we have activated userdisplay.js\n");
db.query('SELECT * FROM User', (error, results, fields) => 
{
    console.log('we did a query');
    if(error) 
    {
        console.log("Error: ",error);
        res.send({
            "code": 400,
            "failed": "Error occurred"
        });
    } else {
        console.log("Results: ",results);
        /*res.send({
            "code": 200,
            "success": "Database successfully logged"
        });*/
        res.render('tempdisplay.html', {data: results});           
    }
});

当我尝试运行它将它们转换为字符串时,它会给我一个&#34;无效的正则表达式:missing / in file&#34;错误。仅当此脚本存在时才会发生此错误。我在文件中包含了jquery和handlebars,而我的其他文件也使用了pathways / cdn。我似乎真的不明白如何使用把手来显示数据。如果我创建另一个js文件,我将无法看到数据对象返回到tempdisplay,但是如果我尝试在html文件中创建一个把手脚本,它会给我语法错误。

1 个答案:

答案 0 :(得分:0)

要在客户端上显示数据,通常的方法是将其插入服务器上的模板中:

// fill tempdisplay.hbs with results, send to client
res.render('tempdisplay', {data: results});

模板文件tempdisplay.hbs的一个例子是:

<table>
    <tbody>
        {{#each data}}
            <tr>
                <td>{{this.fname}}</td>
                <td>{{this.lname}}</td>
            </tr>
        {{/each}}
    </tbody>
</table>