问题描述
我正在尝试将 JSON 对象从 pug 传递到客户端 JavaScript.下面是代码的结构.我渲染了一个 JSON 对象并将它从我的 Node-Express 后端传递给 Pug.代码如下:
I am trying to pass a JSON object from pug to client side JavaScript. Here's how the code is structured. I render a JSON object and pass it to Pug from my Node-Express backend. Code below:
server.js
:
app.get('/myrooms', function(req, res) {
Room.find()
.where('_id')
.in(user.rooms)
.exec(function (err, records) {
res.render('rooms/index', {myrooms : records})
})
})
此后该对象在我的 pug 文件中可用.现在我想将它传递给客户端脚本.我在我的 index.pug
文件中做这样的事情.
After that this object is available in my pug file. Now I want to pass it to a client side script. I am doing something like this in my index.pug
file.
index.pug
:
script(src='/js/play.js').
trooms = "#{myrooms}"
play.js
:
console.log(trooms)
它给了我troom
未定义"错误.我不知道如何传递这个对象.根据一些旧帖子,这是在玉石上工作的.但是,我使用的是 pug 版本 2.0.0-rc.2
.
It gives me 'troom
is not defined' error. I don't know how I can pass this object. According to some old post this was working in jade. However, I am using the pug version 2.0.0-rc.2
.
推荐答案
你可以试试
var trooms = !{JSON.stringify(myrooms)}
这篇关于如何将 Pug JSON 对象传递给客户端 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!