smmaca smmaca - 1 month ago 15
Javascript Question

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


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)

Answer

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.