2

I am fairly new to Javascript and I am having trouble with the code not being executed in a $.getScript call. I've referenced Using $.getScript (jquery): code is not executed and jQuery getScript to be able to answer my questions but have not been able to resolve anything. I'm trying to build a script in order to store information on Backendless so I am trying to test out how to get information onto it first. I've hit a roadblock with trying to get the call to the Backendless.js sdk. I've put in checks in between lines to see where it does not execute. It completely misses the 4th check and skips down to 5. I'm not sure where to go from here and was wondering if anyone knew how to get the script to call the inside code, since in my first reference there was a script that was using $(function(){}). However I'm fairly certain this is not the case in mine.

require("jsdom").env("", function(err, window) {
  console.log("this 1");
  if (err) {
    console.error(err);
    return;
  }
  console.log("this 2");
  var $ = require("jquery")(window);
  console.log("this 3");
  $.getScript('http://api.backendless.com/sdk/js/latest/backendless.js', function()
  {
    console.log("this 4");
    var APPLICATION_ID = '  0',
        SECRET_KEY = '   0',
        VERSION = 'v1'; //default application version;
        Backendless.initApp(APPLICATION_ID, SECRET_KEY, VERSION);
  });
  console.log("this 5");
});
Community
  • 1
  • 1
jehhx
  • 21
  • 1
  • It "skips" the fourth one because `$.getScript` is asynchronous. The function you pass to `$.getScript` won't execute until the file has been loaded. Are you getting any 404 or 500 errors? – Mike Cluck Mar 29 '16 at 18:51
  • @MikeC I do not get any 404 or 500 errors when running it in the terminal window so I am not sure why the file is not loading to be able to execute the function inside. – jehhx Mar 29 '16 at 19:15
  • Seems weird you would use getScript. Why are you not using require? – epascarello Mar 29 '16 at 19:37
  • Works [here](https://jsfiddle.net/k0nzdj6z/) in a browser, but I had to change http to https. – xrgb Mar 29 '16 at 19:44
  • @epascarello based on [this](http://stackoverflow.com/questions/9901082/what-is-this-javascript-require) I understood require to be used for loading javascript modules in node.js and I was certain that the script I am trying to get was not a module. However I tried simply using `require(http://api.backendless.com/sdk/js/latest/backendless.js)` and it tells me that it cannot find the module – jehhx Mar 29 '16 at 19:48
  • @RosHartigan I changed it accordingly in my file and it still does not execute the function in the inside. In the link you provided, how do you know it works? I ran it and nothing showed in the bottom right quadrant – jehhx Mar 29 '16 at 19:56
  • @jehx I used console.log to check Backendless after it was initialized with initApp, and it output a Backendless object. – xrgb Mar 29 '16 at 20:09

1 Answers1

0

Backendless is also an npm module, so you can install it using 'npm install backendless', and then write just

var Backendless = require('backendless')

Without getScript function.