简体   繁体   English

对 json 文件进行更改后,如何让我的 nodejs 文件重新识别?

[英]how can i get my nodejs file to recongnice when a change has been made to a json file?

I'm working on a nodejs program, in the program I need to be able to check a variable inside a JSON file that will be switched via a different program.我正在开发一个 nodejs 程序,在程序中我需要能够检查 JSON 文件中的变量,该变量将通过不同的程序进行切换。 I started working on an if statement to figure it out, I soon found out that nodejs doesn't like it when i go around switching variables after the code has been ran.我开始研究 if 语句来解决这个问题,我很快发现 nodejs 不喜欢它,当我在代码运行后四处切换变量时。 I ran a code that looked a little like this:我运行了一个看起来有点像这样的代码:

"wait for event"{
"code once event happens"
var jsonfile = require('./jsonfile.json')
console.log(jsonfile.variable)}

I would trigger the event, change the json manually then trigger it again the output would look like this我会触发事件,手动更改 json 然后再次触发它,输出将如下所示

true
true

meaning that it would NOT read the json file after i change it这意味着在我更改它后它不会读取 json 文件

I tried to force it to 're require' the JSON file in the code above but that hasn't worked, Ive tried running it in a fresh nodejs script to see if it was because of a statement it could possibly be inside the code looked like this我试图强制它在上面的代码中“需要”JSON文件,但没有奏效,我尝试在一个新的nodejs脚本中运行它,看看它是否是因为它可能在代码中像这样

while (true){
var file = require('file.json')
console.log(file.variable)}

i would change the variable and the output would keep outputting true or false我会改变变量,输出会继续输出truefalse

that didnt work.那没有用。

Geshode wrote a comment that said: Instead of using require, have you tried reading the json file with the native file system, like fs.readFile? Or a library like fs-extra? Geshode 写了一条评论说: Instead of using require, have you tried reading the json file with the native file system, like fs.readFile? Or a library like fs-extra? Instead of using require, have you tried reading the json file with the native file system, like fs.readFile? Or a library like fs-extra?

that worked i put it as an answer so other people can see it, if you need heres some code samples那行得通,我把它作为答案,这样其他人就可以看到它,如果你需要一些代码示例

to read a json file:读取 json 文件:

fs.readFile('pathtojson', 'utf8', function(err, data){
    let student = JSON.parse(data);
    // Display the file content
    console.log(student.avariableinfile);
  })

if edited correctly should output a variable inside of your json file如果正确编辑应该在你的 json 文件中输出一个变量

to read WITHOUT making it a json object在不使其成为 json 对象的情况下阅读

fs.readFile('pathtofile', 'utf8', function(err, data){
    // Display the file content
    console.log(data);
  });

note: the only differences between these samples is the second one dosnt make it into a json file it will do the same thing if you replace data in console.log(data) to student.varable and add JSON.parse(data) above it注意:这些示例之间的唯一区别是第二个示例不会将其放入 json 文件中,如果您将console.log(data) data的数据替换为student.varable并在其上方添加JSON.parse(data) ,它将执行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM