我正在尝试使用.getJSON将.json文件加载到我的程序中,但由于某种原因,它无法加载
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window} = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = jQuery = require('jquery')(window);
...
async function check(){
$.getJSON('test.json', function(result){
console.log("json: " + result);
});
}
async function wait() {
return new Promise(function(resolve) {
setTimeout(resolve, 2000);
});
}
client.on("ready", async () => {
console.log(`Bot is running! ${client.user.username}`);
client.generateInvite(["ADMINISTRATOR"]).then(link =>{
console.log(link);
}).catch(err => {
console.log(err.stack);
});
while(1){
await wait();
await check();
}
});
client.login(setup.key);
这是我正在使用的代码,它旨在每隔几秒钟检查一次某个文件。我想检查文件,以便我可以检测到更改,然后触发一条消息。
答案 0 :(得分:0)
由于这里的注释中提供了帮助,因此找到了一种比使用jQUERY和.getJSON更为简便的解决方案。只需使用readFile和JSON.parse:
function check(){
fs.readFile('test.json', (err, data) => {
if (err) throw err;
json = JSON.parse(data);
console.log(json);
})
}