1

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
  • 7,595
  • 2
  • 12
  • 40
  • 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

1 Answers1

0

Remember to reference your scripts/cdn to jquery before the utilization of jquery itself.

You can use googles by including:

<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

And as @Jon P said, in this case it is much much better to fetch the results using the fetch function instead of an ajax function. An example of such code is listed below.

fetch(`https://keyvalue.immanuel.co/api/KeyVal/GetValue/${database_key}/balance`)
.then(res => res.text())
.then(response => message.channel.send(`Success! ${response} points was added to the world sum!`)
Finbar
  • 530
  • 3
  • 15
  • 1
    Perfect answer. From the error message, it seems clear that he didn't include jQuery. – Haroldo_OK Dec 07 '21 at 10:06
  • @Haroldo_OK Not so perfect. First, you can't add `head` and `script` tags in node.js and while `fetch` seems promising, Finbar doesn't mention that you need to add and import a package for that (like `node-fetch`) as `fetch` is not available in node. – Zsolt Meszaros Dec 07 '21 at 10:17
  • Interesting point there: the question says that they are using `node.js`, but also that they're using `jQuery`... are they, perhaps, trying to use `jQuery` server-side? That wouldn't be a great idea. I'm going to ask. – Haroldo_OK Dec 07 '21 at 12:34
  • This is a Visual Studio Code JavasScript file. I don't know about other people, but mine doesn't have an HTML file. – Charles yao Dec 07 '21 at 20:23
  • 1
    @Charlesyao There is no such thing as _"Visual Studio Code JavasScript file"_. Please check [my comment](https://stackoverflow.com/questions/70254846/im-trying-to-make-a-discord-bot-only-some-features-of-jquery-works-ajax-doesn/70255109?noredirect=1#comment124196847_70254846) on the original post or look into [`node-fetch`](https://www.npmjs.com/package/node-fetch). – Zsolt Meszaros Dec 07 '21 at 20:27
  • 1
    @Zsolt Meszaros, I provided support assuming that this was running in a browser; due to jquery more times than not being run client side instead of server side. – Finbar Dec 07 '21 at 23:39
  • 3
    @Finbar, the question clearly references "Discord bot" which are written in javascript but not browser-based. – Jon P Dec 07 '21 at 23:43