我有以下代码
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);
}
});
});
};
但是会收到此错误
node bin/server
/home/project/src/service/account.js:13
exports.default = (id, api = 'https://api.domain.tld/') => {
^
SyntaxError: Unexpected token =
我错过了什么?
答案 0 :(得分:0)
您无法在您正在运行的环境支持的JS版本中定义默认参数值。
过去常见的处理方式如下:
export const getAccounts = (id, api) => {
api = api || 'https://api.domain.tld/';
// ...
}
编辑:
@Maxx更喜欢这样的东西以获得完美的ES2015兼容性:
export const getAccounts = (id, api) => {
if (api === undefined) {
api = 'https://api.domain.tld/';
}
// ...
}