简体   繁体   English

将对象从node.js发送到ejs,而不是[对象对象]

[英]Send object from node.js to ejs, not [object Object]

If you use node.js and ejs and render JavaScript object to ejs, the resultant HTML page has the following syntax: 如果您使用node.js和ejs并将JavaScript对象呈现为ejs,则生成的HTML页面具有以下语法:

[object Object]

despite the fact that my object is as follows: 尽管我的对象如下:

[{"a": 3, "b": 10}, {"c":3, "d":20}, {"e":1, "f":55}]

However, I want to render the object itself (object literal if I understand it correctly), not the useless [object Object] . 但是,我想呈现对象本身(如果我正确理解,则呈现对象文字),而不是无用的[object Object]

So how can I render it properly? 那么如何正确渲染呢? res.render("index", {result: listOfObject.valueOf()}) didn't work. res.render("index", {result: listOfObject.valueOf()})无效。

[object Object] is what you get when you call .toString() on an anonymous object. [object Object]是在匿名对象上调用.toString()时得到的。 This is implicitly done when you concatenate with another string (eg "my object: " + {a:'b'} ). 当您与另一个字符串(例如"my object: " + {a:'b'} )连接时,将隐式完成此操作。

If you want to get the output you're looking for, you need to use 如果要获取所需的输出,则需要使用

JSON.stringify(yourObjectHere)

Which prints it all out nicely. 哪个可以很好地打印出来。

res.render("index", { result: JSON.stringify(listOfObject) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM