5

I'm getting the error as "async functions' is only available in es8 (use 'esversion 8')" I tried putting "esversion:8" in inline code, package.json but its not working out and function is not getting deployed.

Code: index.js

'use strict';
'use esversion: 8'; //not working

async function getWeather() {
      const city = 'Mumbai';
      const OPENWEATHER_API_KEY = '<API KEY>';


      const response = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${OPENWEATHER_API_KEY}`);
      const data = await response.json();
      console.log(data);

      const { main } = data;
      const { temp } = main;
      const kelvin = temp;
      const celsius = Math.round(kelvin - 273.15);

      console.log(celsius);
   }

Code package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0",
    "esversion":"8"  //not working
  }
}

Error Screenshot

Is there any solution to this or any other way?

  • Does this help https://stackoverflow.com/questions/42637630/does-jshint-support-async-await ? – AlwaysConfused May 24 '20 at 11:42
  • /* jshint esversion: 8 */ add this to your index.js before 'use strict'. Also, you need to enable firebase billing to get a response from an external API – Sairaj Sawant May 24 '20 at 11:52
  • @SairajSawant Its giving the error that **incompatible values for the 'esversion' and 'esnext' linting options** – RUSHABH SHAH May 24 '20 at 16:14
  • can u check this https://www.reddit.com/r/GoogleAssistantDev/comments/dv6y7t/getting_use_esversion_8_but_if_i_do_that_i_get/ – Sairaj Sawant May 25 '20 at 02:03
  • As the Reddit post commented above suggests, try specifying the node.js version as mentioned here: https://firebase.google.com/docs/functions/manage-functions#set_runtime_options – Waelmas May 27 '20 at 12:58

1 Answers1

0

You could convert the function from async/await to a Promise-based function, you can wrap the function body in a Promise and use .then() and .catch() for handling the resolved and rejected states respectively.

function getWeather() { 
  return new Promise((resolve, reject) => {
  const city = 'Mumbai';
  const OPENWEATHER_API_KEY = '<API KEY>';

  fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${OPENWEATHER_API_KEY}`)
  .then(response => response.json())
  .then(data => {
    console.log(data);

    const { main } = data;
    const { temp } = main;
    const kelvin = temp;
    const celsius = Math.round(kelvin - 273.15);

    console.log(celsius);

    resolve(celsius); // Resolve the Promise with the celsius value.
  })
  .catch(error => {
    console.error(error);
    reject(error); // Reject the Promise with the error if any occurs.
      });
  });
}

Then you can use it in this way:

   function WeatherInfoMessage(agent){
   return getWeather(agent)
   .then(celsius => {
            // Use the celsius value here when the Promise is resolved.
          })
   .catch(error => {
            // Handle the error here when the Promise is rejected.
          });      
  }

then, set the intent mapping to WeatherInfoMessage()

  intentMap.set('get weather', WeatherInfoMessage);
Tao
  • 366
  • 1
  • 3
  • 15