comp426-2022-fall / a02

Create a command line Node.js package that ingests API data from Open-Meteo
GNU Affero General Public License v3.0
0 stars 0 forks source link

Runtime error: [Unexpected Reserve Word await when using fetch command] #3

Closed ZachCairns1 closed 1 year ago

ZachCairns1 commented 1 year ago

What is happening?

I've already linked cli.js to galosh.js and am trying to test my code but when I run it, I keep getting an error that highlights the keyword await when calling fetch(url).

What does the error say is happening?

const response = await fetch(url) ^^^^^

SyntaxError: Unexpected reserved word at Loader.moduleStrategy (internal/modules/esm/translators.js:140:18) at async link (internal/modules/esm/module_job.js:42:21)

What do you think the error means?

I looked it up and it says the error happens when you try to call await outside of an async function, but looking at the demos in class and in the assignment, fetch is asynchronous already and the command I'm trying to do is copied from the lab outline so I'm not sure why await is throwing an error.

Command string

galosh.js

(Or can also try node cli.js -h)

Code snippet

import fetch from 'node-fetch';

const response = await fetch(url)

const data = await response.json();

Screenshots

Error log

const response = await fetch(url) ^^^^^

SyntaxError: Unexpected reserved word at Loader.moduleStrategy (internal/modules/esm/translators.js:140:18) at async link (internal/modules/esm/module_job.js:42:21)

Node.js version

v12.22.10

Operating system

Darwin Zachs-MacBook-Air-10.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 x86_64

jdmar3 commented 1 year ago

I think I know what might be happening here, but could post a link to your repository and make sure that you have your current state pushed to it?

ZachCairns1 commented 1 year ago

Yes I can. Here is a link to my repository: https://github.com/comp426-2022-fall/a02-ZachCairns1 It is my current state which has the reserve word error.

ZachCairns1 commented 1 year ago

I've been trying to debug what's going wrong here with the unexpected reserve word error by going back to lecture 06, https://github.com/comp426-2022-fall/schedule/blob/main/06-acquiring-data.md, but even where I literally copy and paste the code and follow along that lecture with the fetch instructions, I get the same error: Unexpected reserve word.

Really not sure what's happening here.

vennila-t commented 1 year ago

Hello Zach,

I looked more into your issue and I believe the problem is with the word "await". Typically, this is resolved by making the enclosing function async and changing the type in your package.json file to module, which you have already done. As for your cli.js, the consts are not enclosed within a function so it is difficult to pinpoint where to write "async". I suggest creating a function and declaring your two const variables in it and them naming the function as async. Some methods in which you can try this are listed on here.

Let me know whether this helps.