繁体   English   中英

我不明白这个语法错误

[英]I don't understand this syntax error

const rootURL = 'http://api.openweathermap.org/data/2.5/weather?APPID=????????';

function kelvinToC(temp) {
  return temp - 273.15;
}

export function getWeather(latitude, longitude) {
  const url = `${rootURL}&lat=${latitude}&lon=${longitude}`;
  return fetch(url).then(res => res.json()).then(json => {
    city: json.name,
->  temperature: kelvinToC(json.main.temp),    // This is line 11                       
    description: json.weather.description,
  });
}

错误显然在11:15,并且缺少分号。 这会将分号放在单词体温的中间。 我究竟做错了什么?

注意:我故意将自己的api密钥清空。 实际的代码中包含api密钥。

错误消息:语法错误/Users/shavaunmacarthur/Documents/react-native-workspace/weather/src/api.js:意外的令牌,应为; (11:15)

我建议在返回对象的周围加上括号:

getWeather(latitude, longitude) {
    const url = `${rootURL}&lat=${latitude}&lon=${longitude}`;
    return fetch(url).then(res => res.json()).then(json => ({
        //                                                 ^
        city: json.name,
        temperature: kelvinToC(json.main.temp),
        description: json.weather.description
        //                                   ^ optional no comma
    }));
//   ^
}

解析器认为您有代码块时发生错误。 这不是故意的,因为您喜欢返回一个对象。 要返回一个对象,您需要

a => {
    return { object: true };
}

要么

a => ({ object: true })

它不以代码块开头。

暂无
暂无

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

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