Error using async in Nodejs

Asked

Viewed 49 times

2

I have a system in Nodejs in development and finished a step, for the purpose of testing, I put it on a server and from that began to return me error in all lines of code in which I used the async functions.

Error:

async index(request, response){
SyntaxError: Unexpected identifier
...

Code:

const Authentication = require('./AdminAuthController');
module.exports = {
async index(request, response){ // esta é a linha do erro
    const { admin } = request.headers;
    const auth = await(Authentication.checkAuthentication(admin));
    return response.json(auth);
}

My biggest problem is finding the reason to return this error, and in the tests I did on the localhost, all requests worked normally.

  • why doesn’t it module.export =async index(request, response){
 const { admin } = request.headers;
 const auth = await(Authentication.checkAuthentication(admin));
 return response.json(auth);
}, you are exporting an object and not a function.

  • Below are other functions

  • 2

    It’s probably a version problem. What version of Node.js do you use on your local computer and what version of the server Node?

  • I think this is the same problem. My computer is version > 10.1 and the server is 6.1

  • 1

    https://medium.com/balta-io/nodejs-async-await-21ca3636252a. Victor please update to 8.0

1 answer

3


The problem is the version of Node.js that you are running on your server. Resources like async/await entered from Node.js 7.6. That way, since you are using the version 6, these features have not yet been implemented, which causes the syntax error.

Because of this, I suggest you upgrade to some version LTS after the version 7.6, like Node.js 8 or 10.

It is important to stress that it is always good to use an LTS version in a production environment. To learn more about the Node.js version scheme and understand why, see this document.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.