0

I'm using a Javascript file to try and get a token from ArcGIS Online. However, whenever I try it, it comes back with

init.js:11 Uncaught Error: undefinedModule

The file (GetAToken.js) is below:

dojo.ready(init);
var request = dojo.require('request'); // npm install request

// generate a token with your client id and client secret
function getToken(callback) {
    request.post({
        url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
        json: true,
        form: {
            'f': 'json',
            'client_id': '<<MY_CLIENT_ID>>',
            'client_secret': '<<MY_CLIENT_SECRET>>',
            'grant_type': 'client_credentials',
            'expiration': '1440'
        }
    }, function (error, response, body) {
        console.log(body.access_token);
        callback(body.access_token);
    });
}

And the bit which calls it (in a HTML file) is:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://esri.github.io/calcite-bootstrap/assets/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://js.arcgis.com/4.0/esri/css/main.css">
<script src="https://js.arcgis.com/4.0/"></script>
<script src="GetAToken.js">
    var MyToken = callback(getToken);
    alert(MyToken);
</script>
user25730
  • 517
  • 2
  • 6
  • 24
  • Do you see a file called `init.js` in your code anywhere? Not 100% the problem, but worth seeing what's in that file.. – Toby Jul 25 '16 at 22:07
  • Nope. I take it i should try and find out how to add one. – user25730 Jul 25 '16 at 22:08
  • Not necessarily - in the error message, it's line 11 of that file where the error occurs. If you can find it, that might give a clue as to what module is undefined. – Toby Jul 25 '16 at 22:11
  • Don't think I can find it. As I say, there is no `init.js` in my code. Though if I hover over it it lists it as `https://js.arcgis.com/4.0/init.js:11` – user25730 Jul 25 '16 at 22:14
  • You call and module (with `require`) and the module never loaded before – YaakovHatam Jul 25 '16 at 22:33
  • Would that indicate that I've got to use a `dojo.require("init");` at the top of the js file? Just quickly tried that and it's still coming back with the same problem. – user25730 Jul 25 '16 at 22:35
  • seperate the code from the script loading: instead of `` write `` – YaakovHatam Jul 25 '16 at 22:42
  • Sorry, still the same problem. – user25730 Jul 25 '16 at 22:44
  • @user25730 - Are you trying to run a script in NodeJS server or a IIS/tomcat server? – T Kambi Aug 01 '16 at 18:30

1 Answers1

0

It looks like you are trying to get the requestJS through NodeJS (npm install request). I am right?

You need to be aware that NodeJS require uses CommonJS whereas dojo uses RequireJS. Both of them have different module structures in them. More details here

In the line var request = dojo.require('request'); It is not able to find request module so it is throwing the error.

The way to get nodejs modules in dojo is to use dojo/node as show below.

require([ "dojo/node!request" ], function(request){
    // Utilise the "request" module
});

Go through the Tutorial for Dojo and Node.js

Hope this was helpful.

PS: Esri has its own request object (esri/request), which you can use to get tokens. You may want to use that instead.

Community
  • 1
  • 1
T Kambi
  • 1,389
  • 2
  • 9
  • 16