Mastef Chief
Mastef Chief

Reputation: 31

Function returning undefined variable Javascript

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();
  }
}

Upvotes: 1

Views: 535

Answers (3)

r-sede
r-sede

Reputation: 88

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
})

Upvotes: 0

Andria
Andria

Reputation: 5075

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';

Upvotes: 2

himank
himank

Reputation: 439

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.

Upvotes: 1

Related Questions