【发布时间】:2017-12-15 08:39:34
【问题描述】:
我正在使用 nodeJS express ejs。我的 JSON 有问题,在呈现我的 ejs 文件之前没有填充数据。
app.get('/', function(req, res) {
fs.readFile('somepath', 'utf8', function(errRead, data) {
var obj;
if(errRead) {
obj = {loaded : false};
} else {
var nbCam = 0;
fs.readFile('somepath', 'utf8', function(errRead, data) {
if(errRead) throw errRead;
for(var i in JSON.parse(data).currentAcquisitionSet) {
++nbCam;
}
obj = {loaded : nbCam, images : JSON.parse(data).images};
});
}
console.log(obj); // why is it undefined here?
res.render('a.ejs', obj);});
});
});
看起来我的 obj 是一个局部变量,但我不明白为什么。
【问题讨论】: