简体   繁体   中英

Unexpected token, javascript =

I have the following code

import request from 'request-json';

export const getAccounts = (id, api = 'https://api.domain.tld/') => {
  return new Promise((resolve, reject) => {
    const client = request.createClient(api);

    client.get(`accounts/${id}/full`, (err, res, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

but get this error

node bin/server    
/home/project/src/service/account.js:13
exports.default = (id, api = 'https://api.domain.tld/') => {
                           ^

SyntaxError: Unexpected token =

what am i missing?

You can't define default parameter values like that in the version of JS supported by the environment that you're running.

A common way of handling this in the past would look something like this:

export const getAccounts = (id, api) => {
    api = api || 'https://api.domain.tld/';
    // ...
}

EDIT:

@Maxx would prefer something like this for perfect ES2015 compatibility:

export const getAccounts = (id, api) => {
    if (api === undefined) {
        api = 'https://api.domain.tld/';
    }
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM