0
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,
  });
}

The error is apparently at 11:15 and is a lack of semicolon. This puts the semicolon in the middle of the word temperature. What am I doing wrong?

Note: I have blanked out my api key on purpose. The actual code has the api key in it.

Error message: Syntax error /Users/shavaunmacarthur/Documents/react-native-workspace/weather/src/api.js: Unexpected token, expected ; (11:15)

smmaca
  • 85
  • 1
  • 5
  • 1
    "The error is apparently at 11:15 and is a lack of semicolon." — What **exactly** does the error message say? – Quentin Oct 30 '16 at 08:34
  • Don't include *pictures* of errors (or code), include the **text**. Also, tell us what `11:15` refers to (just point to line 11 - I think your code above is complete, but we can't know that, so counting down from the top may not be reliable). – T.J. Crowder Oct 30 '16 at 08:39
  • I understand that the error message was initially missing, but why the downvote after it was added? (Yes, yes, it should be text, but...) Shavan - Don't take the voting the wrong way. You'll know for next time, and this was **not** a poor question. (Still worth editing to show the error message as text, even though the question has been answered -- for others in the future.) – T.J. Crowder Oct 30 '16 at 08:44
  • Sorry, I didn't realise a picture was not sufficient. It wouldn't let me copy-paste the error from the simulator obviously and I was in a rush to get it sorted. The code is the complete file but an arrow pointing to the line would have been a good idea. – smmaca Oct 30 '16 at 08:47
  • Duplicate of http://stackoverflow.com/questions/28770415/ecmascript6-arrow-function-that-returns-an-object. –  Oct 30 '16 at 08:56
  • @torazaburo, now, it's obvious. – Nina Scholz Oct 30 '16 at 08:58

1 Answers1

3

I suggest adding parentheses around the return object:

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
    }));
//   ^
}

The error occurs, while the parser is thinking you have a code block. This is not intended, because you like to return an object. To return an object, you need either

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

or

a => ({ object: true })

which does not start as a code block.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392