Мой код
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();
Я получил
(node:15582) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received an instance of Object
Я не понимаю. Я посмотрел на этот вопрос . Где я должен установить process.on в своем коде?
Я пытался изменить свой код
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))
)
В любом случае, та же проблема.
(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)
})
}
Как бороться с этой проблемой?