1

My code

var axios = require("axios");
var userDetails;

function initialize() {
    var options = {
        url: "https://api.github.com/users/narenaryan",
        headers: {
            'User-Agent': 'axios'
        }
    };
    return new Promise(function(resolve, reject) {
        axios.get(options, function(err, resp, body) {
            if (err) {
                reject(err);
            } else {
                resolve(JSON.parse(body));
            }
        })
    })

}

function main() {
    var initializePromise = initialize();
    initializePromise.then(function(result) {
        userDetails = result;
        console.log("Initialized user details");
        // Use user details from here
        console.log(userDetails)
    }, function(err) {
        console.log(err);
    })
}

main();

I got

(node:15582) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received an instance of Object

I don't get it. I looked at this SO question. Where should I set the process.on in my code?

I tried to change my code

function initialize() {
    var options = {
        url: "https://api.github.com/users/narenaryan",
        headers: {
            'User-Agent': 'axios'
        }
    };
    return new Promise(function(resolve, reject) {
        axios.get(options) 
        .then(()=> resolve(JSON.parse(body))
        .catch(err => console.error(err))
        )

Anyway,the same problem.

(node:17021) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received an instance of Object
    at validateString (internal/validators.js:120:11)
    at Url.parse (url.js:159:3)
    at Object.urlParse [as parse] (url.js:154:13)
    at dispatchHttpRequest (/home/miki/examples/prom1/node_modules/axios/lib/adapters/http.js:66:22)
    at new Promise (<anonymous>)
    at httpAdapter (/home/miki/examples/prom1/node_modules/axios/lib/adapters/http.js:21:10)
    at dispatchRequest (/home/miki/examples/prom1/node_modules/axios/lib/core/dispatchRequest.js:52:10)
(node:17021) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

})

}

How to deal with this problem?

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121

1 Answers1

0

It looks like the first argument for the axios get function should be a url string.

Remove the url from your options object and try modifying your code as shown below:

....
const url = "https://api.github.com/users/narenaryan"

axios.get(url, options, function(err, resp, body) {
...
mprather
  • 170
  • 1
  • 1
  • 8