简体   繁体   English

意外令牌,javascript =

[英]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. 您无法定义所运行环境所支持的JS版本中的默认参数值。

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: 为了实现完美的ES2015兼容性,@ Maxx会喜欢这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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