gpt4 book ai didi

javascript - Express 和 ejs <%= 渲染 JSON

转载 作者:IT老高 更新时间:2023-10-28 12:43:38 30 4
gpt4 key购买 nike

在我的 index.ejs 我有这个代码:

var current_user = <%= user %>

在我的节点中

app.get("/", function(req, res){
res.locals.user = req.user
res.render("index")
})

但是,在我获得的页面上

var current_user = [object Object]

如果我写了

var current_user = <%= JSON.stringify(user) %>

我得到:

var current_user = {&quot;__v&quot;:0,&quot;_id&quot;:&quot;50bc01938f164ee80b000001&quot;,&quot;agents&quot;:...

有没有办法传递 JS 可读的 JSON?

最佳答案

哦,这很简单,不要使用 <%= , 使用 <%-反而。例如:

 <%- JSON.stringify(user) %>

第一个将呈现在 HTML 中,第二个将呈现变量(因为它们是 eval)

关于javascript - Express 和 ejs <%= 渲染 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788314/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com