I am trying to load a .json file into my program using .getJSON but for some reason it wont load
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);
This is the code i am using, it is meant to check a certain file every few seconds. I want to check the file so that i can detect changes and then trigger a message.
Thanks to the help in the comments here, found a way easier solution other then using jQUERY and .getJSON. Just use readFile and JSON.parse:
function check(){
fs.readFile('test.json', (err, data) => {
if (err) throw err;
json = JSON.parse(data);
console.log(json);
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.