Reputation: 7746
I am following this example, but on this section of code...
const getApiAndEmit = async socket => {
try {
const res = await axios.get(
"https://api.darksky.net/forecast/PUT_YOUR_API_KEY_HERE/43.7695,11.2558"
); // Getting the data from DarkSky
socket.emit("FromAPI", res.data.currently.temperature); // Emitting a new message. It will be consumed by the client
} catch (error) {
console.error(`Error: ${error.code}`);
}
};
I get the following error
D:\Documents\js\socketio-server\app.js:42
const getApiAndEmit = async socket => {
^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
PS D:\Documents\js\socketio-server>
Upvotes: 0
Views: 701
Reputation: 783
The function syntax seems correct. You may need to update node, as async
support didn't arrive until early this year (edit: version 7.6 after googling).
You can rewrite using promises or try the --harmony
flag when running from the command line.
Upvotes: 1
Reputation: 938
should enclose function with parentheses ()
const getApiAndEmit = async (socket => {
try {
const res = await axios.get(
"https://api.darksky.net/forecast/PUT_YOUR_API_KEY_HERE/43.7695,11.2558"
); // Getting the data from DarkSky
socket.emit("FromAPI", res.data.currently.temperature); // Emitting a new message. It will be consumed by the client
} catch (error) {
console.error(`Error: ${error.code}`);
}
});
Upvotes: 1