Function returning undefined variable Javascript
Function returning undefined variable 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();
}
}
obj
apologies forgot to remove that, it was there in error when i was pasting here
– Mastef Chief
37 mins ago
Possible duplicate of How do I return the response from an asynchronous call?
– Patrick Roberts
16 mins ago
3 Answers
3
The issue you're having is caused by not returning any value at all from the function getTechnicPackInfo, not the inner functions.
getTechnicPackInfo
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:
fetch
fetch
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
Promise
.json
JSON.parse
Promise
fetch
fetch is not a default function built into node.js, therefore, you have to import it after installing node-fetch.
fetch
node-fetch
npm install --save node-fetch
import fetch from 'node-fetch';
There's nothing wrong with using
node-fetch– chbchb55
16 mins ago
node-fetch
Okay, I will fix that.
– chbchb55
14 mins ago
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.
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
})
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why are you returning a function call? Shouldn't you just be returning
obj?– Andy Hoffman
39 mins ago