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

心已入冬 提交于 2020-01-16 19:28:26

问题


If you use node.js and ejs and render JavaScript object to ejs, the resultant HTML page has the following syntax:

[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].

So how can I render it properly? res.render("index", {result: listOfObject.valueOf()}) didn't work.


回答1:


[object Object] is what you get when you call .toString() on an anonymous object. This is implicitly done when you concatenate with another string (e.g. "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.




回答2:


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


来源:https://stackoverflow.com/questions/19793088/send-object-from-node-js-to-ejs-not-object-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!