0

Fix: The issue was, as Phil pointed out, using AJAX to try and load a JSON that was already in the same folder as the code. All I had to do was remove the $.getJSON() command and use var truthpg = require('./truthpg.json'). Thanks to all who helped me fix this!


Updated code to display errors:

function truthCommand(arguments, recievedMessage) {
    var truthpg = new Array();
    function loadQuestions() {
        $.getJSON("truthpg.json", function(data) {
            truthpg = data;
        })
        .fail(function(data, jqXHR, textStatus, errorThrown) {
            console.error(textStatus, errorThrown)
            console.log("load failed");
            recievedMessage.channel.send('load failed');
        });
    }
    loadQuestions()
} 

Result

enter image description here


Original question/code: I've been trying to code a Discord bot using node.js and discord.js. The code causing issues is:

function truthCommand(arguments, recievedMessage) {
    var truthpg = new Array();
    function loadQuestions() {
        $.getJSON("truthpg.json", function(json) {
            truthpg = json;
        })
        .fail(function() {
            console.log("load failed");
            recievedMessage.channel.send('load failed');
        });
    }
    loadQuestions()
}   

And the test JSON I'm trying to retrieve is:

[
    "tpg1",
    "tpg2",
    "tpg3",
    "tpg4",
    "tpg5"
]

Everything I've tried hasn't stopped the "fail" code from executing. What should I fix/do next to load the JSON properly and be able to add code that uses the array? This is only my second JavaScript project, so I'm not very familiar with the language and how to use it.

Colin Hanes
  • 106
  • 2
  • 12
  • jQuery passes useful information to the `fail` callback so try logging some of that, eg `.fail(function(jqXHR, textStatus, errorThrown) { console.error(textStatus, errorThrown) })`. Also check your browser's developer console for errors as well as the _Network_ tab to see what's going on with the request / response – Phil Mar 25 '20 at 00:49
  • I'm not using any sort of browser to run code in. I'm using powershell to run the program through node. Here is what displayed when I added the extra arguments to the function: https://imgur.com/a/xLt2e6e. I'm not sure what that means is wrong and all answers on the internet vary wildly. – Colin Hanes Mar 25 '20 at 01:12
  • Ah of course, you did say it was a Discord bot. My apologies. Could you please update the code in your question to match what you currently have? – Phil Mar 25 '20 at 01:14
  • I think the issue is probably that you're trying to us AJAX to load a local file. See https://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js – Phil Mar 25 '20 at 01:18
  • 1
    Your arguments to the `fail` callback are incorrect. There are only 3 arguments passed, `function(jqXHR, textStatus, errorThrown)`. There is no `data` argument – Phil Mar 25 '20 at 01:22
  • 1
    ... and please, include what is displayed in console, not just the code – Igor Mar 25 '20 at 01:23
  • I could only get the console to log anything when I added the data argument. With only the three, the console wouldn't log anything and the message send command after that wouldn't execute. Strange, but I probably did something wrong. Also, I linked an imgur showing what the console displayed. – Colin Hanes Mar 25 '20 at 01:28
  • `console.log(data.responseText);` - ? – Igor Mar 25 '20 at 01:32

0 Answers0