如果路径“ /”被调用,我将通过python进程中的RPC收集一些数据,这确实很好。我想使用此JSON对象在HTML(ejs)中显示一些值。实际上它正在工作,但有时会出现以下错误:
temperature is not defined
所以我到目前为止:
app.get('/', function(req, res) {
client.invoke("data", function(error, data, more) {
// console.log(data): {"temperature": "12", "minTemperature": "1", "maxTemperature": "101"}
data = JSON.parse(data);
// console.log(data): { temperature: '12', minTemperature: '1', maxTemperature: '101' }
res.render('index', data);
});
});
我如何在.ejs文件中显示值:
<h4 id="temperature"><%- temperature %> °C</h4>
所以有了这个JSON.parse我就可以正常工作了。现在的问题是,在Chrome中,它总是引发我这个错误。在Firefox中,它可以正常工作,但是如果按CTRL + F5,则会引发此错误。 (仅F5起作用)。有趣的是,即使出现错误,温度也能正确显示。燃烧了几个小时后,我需要您的帮助...
错误:
temperature is not defined
at eval (eval at compile (/home/dominik/Schreibtisch/picam>/node/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:37:17)
at returnedFn (/home/dominik/Schreibtisch/picam/node/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/dominik/Schreibtisch/picam/node/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/home/dominik/Schreibtisch/picam/node/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/dominik/Schreibtisch/picam/node/node_modules/express/lib/view.js:76:8)
at Function.app.render (/home/dominik/Schreibtisch/picam/node/node_modules/express/lib/application.js:561:10)
at ServerResponse.res.render (/home/dominik/Schreibtisch/picam/node/node_modules/express/lib/response.js:845:7)
at /home/dominik/Schreibtisch/picam/node/server-tcp.js:50:7
at callbacks (/home/dominik/Schreibtisch/picam/node/node_modules/express/lib/router/index.js:164:37)
at param (/home/dominik/Schreibtisch/picam/node/node_modules/express/lib/router/index.js:138:11)
答案 0 :(得分:0)
我认为您需要做data.temperature
,因为您返回data
而不是temperature