0

I'm completely aware that this is probably covered in another thread however, i'm new to JavaScript and Node and not completely sure what i'm looking for.

I'm working with a presentation layer called Domo and attempting accessing their API in order to export data. Part of accessing their data is to establish a token to connect with.

I've successfully been able to make the call and console log the value, but i'm unable to push the value to an object such as an Array. I believe this is an anonymous function and been searching all over trying to find a way to use it. My research has been everything from closures, callbacks, let, return and immediately invoked functions but i'm missing something here. Can anyone point me in the right direction?

**Here is the code:**
var http = require("https");

var tokenoptions = {
"method": "GET",
  "hostname": "api.domo.com",
  "port": null,
  "path": "/oauth/token?grant_type=client_credentials&scope=data%20user",
  "headers": {
              "Accept":"application/json",
              "Authorization": "Basic MyClientAndSecretGoesHere"
              }
}

var req = http.request(tokenoptions, function (res) {
  var chunks = [];
  var token = [];

 res.on("data", function (chunk) {
                    chunks.push(chunk);

                                  });

 res.on("end", function () {
                body = Buffer.concat(chunks);
                 console.log(body.toString());
                           });

});
req.end();  

**End Code**

The Console.log(body.toString()) is the value i want but how do i assign this to an array or object? Is this an anonymous function?

I'd really appreciate the help and apologize in advance if this is a duplicate post. With the age of this site, I don't see how any new post couldn't be.

nblack8
  • 1
  • 1
  • 2
    Why do you need to store this in an array? Why can’t you just store it in a variable? – Scott Marcus Nov 05 '17 at 21:55
  • 1
    To assign it to an array: `var arr = [body.toString()]`. Remember, whatever you want to do with that value, you will have to do it from within that callback function. – JJJ Nov 05 '17 at 21:56
  • It doesn't matter that the function is anonymous, it matters that it's called *asynchronously*. – Bergi Nov 05 '17 at 22:20
  • Thank you Guys, the reason I'm thinking it should be am array is the number of elements returned. The requested token is received as a JOSN containing all the elements for the next HTTP request. Hope I'm saying the correctly, below is an example of the return values. { "access_token":"NewToken” "token_type":"bearer", "expires_in":3599, "scope":"data user", "customer":"MyURL", "env":"prod3", "userId":”MyUser”, "role":"Admin", "jti":"SomeCodeHere" } – nblack8 Nov 06 '17 at 16:42

1 Answers1

0

If I understand correctly, you would want the data to be a Javascript object to be used outside of the callback. I had read the Domo API docs and I hope this works for you:

var http = require("https");

var tokenoptions = {
    "method": "GET",
    "hostname": "api.domo.com",
    "port": null,
    "path": "/oauth/token?grant_type=client_credentials&scope=data%20user",
    "headers": {
        "Accept":"application/json",
        "Authorization": "Basic MyClientAndSecretGoesHere"
    }
}

var req = http.request(tokenoptions, function(res) {
    var chunks = [];
    var token = [];

    res.on("data", function(chunk) {
        chunks.push(chunk);
    });

    res.on("end", function() {
        body = Buffer.concat(chunks);
        nextAction(JSON.parse(body.toString()));
    });
});

req.end();

function nextAction(obj) {
    console.log(obj);
}
AngYC
  • 3,051
  • 6
  • 20
  • Thank you! The above answer worked perfectly. I'm new here but really appreciate the help getting past this setback. I also found placing a return function allowed me to retain the value. res.on("end", function () { body = Buffer.concat(chunks); Token Function(returntoken){ return Token(); } //console.log(body.toString()); }); }); req.end(); – nblack8 Nov 06 '17 at 16:57