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?...

问题:

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

回答1:

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

回答2:

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.


回答3:

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
})
  • 发表于 2018-07-06 02:11
  • 阅读 ( 357 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除