2

So, I'm trying to make a (my first) Discord Bot. One command I'm coding is that each time a command is run, it adds a random number (500-1000) to a database (https://keyvalue.immanuel.co//). I tested out the ajax command for jQuery on a small testing Replit, and it worked just fine. However, when I run THE EXACT SAME CODE on Visual Studio:

$.ajax({
    type: "GET",
    url: `https://keyvalue.immanuel.co/api/KeyVal/GetValue/${database_key}/balance`,
    success: function (response) {
        message.channel.send(`Success! ${response} points was added to the world sum!`
    }
});

It gives me an error:

ReferenceError: $ is not defined

I'm so frustrated, because the following code from jQuery DOES work on my module, only ajax doesn't!

console.log(`I can use jquery to do the easy concatenation thing ${database_key}`)

And one last thing to remember: I ran the exact same AJAX code on Replit and it worked.

MrMythical
  • 8,908
  • 2
  • 17
  • 45
Charles yao
  • 39
  • 1
  • 4
  • 2
    Your second statement is **NOT** jquery, just good old javascript. You most likely haven't included jquery based on that error. Further more one on the main features of jquery is browser DOM manipulation and traversal, it question it's value in a discord bot – Jon P Dec 07 '21 at 04:15
  • 1
    Contemplare using node-fetch instead of jquery ajax : https://discordjs.guide/additional-info/rest-api.html#random-cat – Jon P Dec 07 '21 at 04:31
  • 1
    I've got no idea why anyone would use jQuery today, especially for a simple GET request, but this answers your question: [Nodejs Uncaught ReferenceError: $ is not defined after var jQuery = require('jquery');](https://stackoverflow.com/questions/45343876/nodejs-uncaught-referenceerror-is-not-defined-after-var-jquery-requirejqu) – Zsolt Meszaros Dec 07 '21 at 09:59
  • It seems almost clear that jQuery wasn't included correctly. We could help you better if you show the relevant parts of the page, where jQuery is included. – Haroldo_OK Dec 07 '21 at 10:10
  • An interesting point there: the question says that `node.js` is being used, but also that the code is using `jQuery`... are you, perhaps, trying to use `jQuery` server-side? That wouldn't be a great idea; `jQuery` is designed to facilitate usage browser-side, but it would be a bad idea to use it in a server. – Haroldo_OK Dec 07 '21 at 12:36

0 Answers0