问题描述
如果使用node.js和ejs并将JavaScript对象呈现为ejs,则结果HTML页面具有以下语法:
If you use node.js and ejs and render JavaScript object to ejs, the resultant HTML page has the following syntax:
[object Object]
尽管我的对象如下:
[{"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]
.
那么我该如何渲染正确吗? res.render( index,{结果:listOfObject.valueOf()})
不起作用。
So how can I render it properly? res.render("index", {result: listOfObject.valueOf()})
didn't work.
推荐答案
[对象对象]
是调用 .toString()在匿名对象上。当您与另一个字符串连接时(例如
我的对象: + {a:'b'}
),将隐式完成此操作。
[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)
可以很好地打印出来。
这篇关于将对象从node.js发送到ejs,而不是[对象对象]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!