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?
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.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With