【发布时间】:2017-12-08 22:48:55
【问题描述】:
我最近开始制作 node.js 应用程序,并且已经到了需要读取刚刚更新但未成功的 JSON 值的地步,我尝试使用“睡眠”模块停止线程稍有延迟,但仍然没有运气。
我也没有很长时间使用 node.js,只有大约 5 个月。
var obj;
let sData = ('./session-data/data.json');
var contents = fs.readFile(sData, 'utf8', function(err, data){
if (err) throw err;
obj = JSON.parse(data);
});
// Below is getting input text from an inquirer question & logging it to the json but when console.log 'ed it outputs the last value from the key in the json
fs.writeFile('./session-data/data.json', `{\n "video": "` + answers.input + `"\n}`, function(err){
if(err){
return console.log(err);
}
console.log('Video: ' + obj.video)
});
【问题讨论】:
-
可以分享代码吗?
标签: javascript json node.js asynchronous