1

I'm trying to load my javascript file in Mongo. However I'm getting the following error:

ReferenceError : XMLHttpRequest is not defined

The Javascript file, seems to function fine and I'm not getting any errors in my browser.

Here's the code related to the request:

  var xmlhttp = new XMLHttpRequest();
  var url = "https://omgvamp-hearthstone-v1.p.mashape.com/cards";

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var result = JSON.parse(xmlhttp.responseText);
        updateCardsCollection(result);
        console.log(result);
    }
  };
  xmlhttp.open("GET", url, true);
  xmlhttp.send();

This is the line I'm using to load the file:

load("/myPath/database.js")

Note: I'm not using any frameworks yet. I just have the one Javascript file, trying to implement an api and save it to a mongo database.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
MLyck
  • 4,959
  • 13
  • 43
  • 74
  • 1
    Are you trying to do this in the `mongo` shell? That is not a "full" programming environment. If you want to "program" in JavaScript, use something like `nodejs` instead. – Neil Lunn Apr 10 '16 at 08:21
  • Yes i am trying to load the Javascript file in a mongo shell. (I am somewhat following a tutorial by TeamTreeHouse, except their don't have an API to supply the data, which seems to be causing the problem). Do I need a full fledged programming environment to do this? – MLyck Apr 10 '16 at 09:18
  • 1
    Yes you need a proper programming environment to do this. It also seems you might be "mixing" tutorials here. `XMLHttpRequest()` is typically a "browser" artifact, so I think you need to take a hard look at the tutorials you are following and what you intend to do. I think you are missing the point that "part" of the code is for the "browser" client, and another part of the code would be for the "server". So I would re-read it carefully, and/or switch to another tutorial that "clearly" explains which tools to use and where. – Neil Lunn Apr 10 '16 at 10:40
  • Possible duplicate of [XMLHttpRequest module not defined/found](http://stackoverflow.com/questions/32604460/xmlhttprequest-module-not-defined-found) – Jamie Birch Mar 20 '17 at 12:37
  • Possible duplicate of [Why can I not use new XMLHttpRequest on my amazon server?](https://stackoverflow.com/questions/37609064/why-can-i-not-use-new-xmlhttprequest-on-my-amazon-server) – Paul Sweatte Jun 21 '17 at 14:49

0 Answers0