Nick Laing
Nick Laing

Reputation: 48

Dialogflow: Is this being used in an async call

I'm new to the javascript world and have been tinkering with Actions on Google. I had an action that was previously running but after attempting to simplify my code, I've been running into a new error I cannot seem to figure out. The code is supposed to make a call to a database and return information to the user based on the date they have selected.

Problem: The code is supposed to make a call to a database and return information to the user based on the date they have selected. The intent seems to break down when I call the URL using axios. When I test my function I receive the following error from the Google Cloud Platform: "Error: No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"

app.intent('Moon', (conv, {date}) => {
  // Sets date to today if none given
  if (!date) {
    date = new Date().toISOString();
  }
  // Slices date string to match date format in database
  const dateSlice = date.slice(5, 9);
  // Call to row in dabase for the given date
  function getData() {
    return axios.get(`example.com/search?Date=${dateSlice}`);
  }
  return getData().then(res => {
    res.data.map(con => {
      conv.ask(`On X date there will be a ${con.Object1}`);
    });
  });
});

I don't know much about Promise and await but that seems to be the issue. I'm not sure how I was able to get my code to run before without these objects. I've tried to insert a Promise object before my return but it makes the rest of the function unreachable. I also checked to see if Axios had any updates but it has not, I am on the latest version. Does the error have to do with one of the returns perhaps?

Upvotes: 1

Views: 182

Answers (1)

Prisoner
Prisoner

Reputation: 50701

It could be related to Promises, but you seem to be handling them correctly.

  • The call to axios.get() is returning a Promise...
  • ... which you are returning in getData()...
  • ... which is returned in the 'Moon' Intent handler as part of the getData().then() block.

I suspect more that this is a logic problem. If res.data is an empty array, then there will be no calls to conv.ask(), so you end up not asking anything.

There is also a problem if res.data has more than two items. In this case, you'll generate an error because you've replied with more than two "simple" responses.

Either way - you may wish to log res and/or res.data to make sure you're getting back what you think.

Upvotes: 1

Related Questions