1

In Javascript, when i call this function from another js file i get undefined in my console output. Can I return the json object know as "obj" and not have it be undefined?

module.exports = {
  getTechnicPackInfo: function(id){
    var http = require("https");

    var options = {
      "method": "GET",
      "hostname": "solder.io",
      "path": "/api/aurora-crafters/modpacks/" + id,
      "headers": {
        "accept": "application/json",
        "content-type": "application/Json",
        "authorization": "Bearer 00000000000000000000000000000"
      }
    };

    var req = http.request(options, function (res) {
      var chunks = [];
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });

      res.on("end", function () {
        var body = Buffer.concat(chunks);
        var obj = JSON.parse(body);    
        return obj; 
      });
    });

    req.end();
  }
}
3
  • 1
    Why are you returning a function call? Shouldn't you just be returning obj? Commented Jul 5, 2018 at 1:47
  • apologies forgot to remove that, it was there in error when i was pasting here Commented Jul 5, 2018 at 1:49
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Jul 5, 2018 at 2:10

3 Answers 3

2

You didn't return anything

The issue you're having is caused by not returning any value at all from the function getTechnicPackInfo, not the inner functions.

What you want to do is use the fetch api to fetch all of the stuff you are trying to get and return the value returned by fetch. This allows you to do the following:

getTechnicPackInfo(...).then(resultingData => resultingData.json()).then(parsedData => ...)

This works because fetch returns a Promise allowing you to then parse the data with .json (it has it's own function because you can't simply do JSON.parse) which returns another Promise


fetch doesn't exist in Node by default

fetch is not a default function built into node.js, therefore, you have to import it after installing node-fetch.

Command Line:

npm install --save node-fetch

Node.js:

import fetch from 'node-fetch';
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I will fix that.
1

You are not returning anything from the getTechnicPackInfo function.

What you can do is to use util.promisify your http request and then return the promise.

Comments

0

the answer from above is correct but the api fetch is not present in node env, there is a module npm to add it: "node-fetch".

if you do not want to install this module a solution would be to add a callback argument to the function getTechnicPackInfo() :

file1.js:

module.exports = {
    getTechnicPackInfo: function(id, callback){
        var http = require("https");
        var options = {
            "method": "GET",
            "hostname": "solder.io",
            "path": "/api/aurora-crafters/modpacks/" + id,
            "headers": {
                "accept": "application/json",
                "content-type": "application/Json",
                "authorization": "Bearer 00000000000000000000000000000"
            }
        };
        var req = http.request(options, function (res) {
            var chunks = [];

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

            res.on("end", function () {
                var body = Buffer.concat(chunks);
                var obj = JSON.parse(body);    
                callback(obj); 
            });
        });
        req.end();
    }
}

file2.js

var f = require('./file1.js');
f.getTechnicPackInfo(123456, function(obj) {
    //do what you need with obj
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.