Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected Token async ()

I am having a strange issue, as my code is working fine in Ubuntu and Windows machines and is failing in a Centos server. I have the same node version 8.9.1 and the same npm 5.5.1 and the same sails 1.0.0.41 (globally and locally). Everything works except on my Centos machine where I get

const makeRequest = async () => {
                          ^
SyntaxError: Unexpected token (

with an arrow pointing to the first paren. The only thing that I currently suspect is that my N version management has not properly updated node. Running node -v reports 8.9.1. Here is a simplified cut of the async code:

const makeRequest = async () => {
  try{
    const user = await sails.models.user.findOne({id: user_id});
    return Promise.resolve(user);
    }
  catch(error){
    sails.log.error('error getting data', error);
  }
}

return makeRequest().then(out => {
  return Promise.resolve(out);
});

Any suggestions on how to resolve this error?

like image 301
edencorbin Avatar asked Nov 22 '17 12:11

edencorbin


People also ask

How do I fix unexpected token error?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What does it mean when it says unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

Why is else an unexpected token?

Like other programming languages, JavaScript has define some proper programming rules. Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + – var if-else var etc}. Unexpected token is similar to syntax error but more specific.

What is unexpected token error in php?

Various parts of the PHP language are represented internally by tokens. A code snippet that contains an invalid sequence of tokens may lead to errors like Parse error: syntax error, unexpected token "==", expecting "(" in script. php on line 10." where token == is internally represented by T_IS_EQUAL .


2 Answers

There is missing } in the code. Check below

const makeRequest = async() => {
  try {
    const user = await sails.models.user.findOne({
      id: user_id
    });
    return Promise.resolve(user);
  } catch (error) {
    sails.log.error('error getting data', error);
  }
} // -> Its misssing in your code
return makeRequest().then(out => {
  return Promise.resolve(out);
});

Edited

OP's response

The issue was version management.

node -v gave me 8.9.1 sudo node -v gave me 6.11

The solution was to chown the folder for my user (rather then root), and run the application without sudo. NVM then worked correctly. Accepting the other answer as there were errors in my code.

like image 142
Vipin Kumar Avatar answered Sep 20 '22 04:09

Vipin Kumar


The issue was version management.

node -v gave me 8.9.1 sudo node -v gave me 6.11

The solution was to chown the folder for my user (rather then root), and run the application without sudo. NVM then worked correctly. Accepting the other answer as there were errors in my code.

like image 36
edencorbin Avatar answered Sep 19 '22 04:09

edencorbin