Getting Error | JavaScript, Node.js, React.js and Angular.js Forum
Y
Yogesh Naik Posted on 18/03/2023

Here is JS script -

const requestToGithub = async (login) => {
  let response = await fetch(`https://api.github.com/users/${login}`);

  let json = await response.json();
  let summary = `${json.id},${json.blog}`;

  console.log(summary);
};

requestToGithub("raj");
---------------
Here is html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
</head>
<h1>Hello</h1>

<body>
    <script type="module" src="AsyncWait.js"></script>
</body>
</html>

Y
Yogesh Chawla Replied on 18/03/2023

The same code is printing id as 22039 and blog as "" in the console log of the browser as it is there in the below url:

https://api.github.com/users/raj

So it works fine for me as well.

 

 


Y
Yogesh Naik Replied on 18/03/2023

Hi Yogesh - I am getting error for same program as ----

 

PS C:\Users\Yogesh\Desktop\JS_course\async> node .\AsyncWait.js
(node:16500) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
at requestToGithub (C:\Users\Yogesh\Desktop\JS_course\async\AsyncWait.js:5:18)
at Object.<anonymous> (C:\Users\Yogesh\Desktop\JS_course\async\AsyncWait.js:13:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:16500) 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: 1)
(node:16500) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


Y
Yogesh Chawla Replied on 18/03/2023

Why are you running this with node command?

You have created a HTML and a script and you have added a script directly in the HTML itself.

Just run the HTML page directly with the live server extension in visual studio or directly and see the console log

The summary is printed there itself.


Y
Yogesh Chawla Replied on 18/03/2023

If you want to run Js file directly using Node command, Check the node version first using

node -v

in visual studio

If you're using a version of Node prior to 18, the fetch API was not there then you'll need to use an external module for that, like node-fetch which can installed like this in visual studio

npm install node-fetch

Otherwise download the latest version of node from nodejs.org and check with that


Y
Yogesh Naik Replied on 18/03/2023

Browser console is giving this error -

New.html:1

Access to script at 'file:///C:/Users/YH/Desktop/JS_course/async/AsyncWait.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome-untrusted, https, edge.