Francisco Navarrete
Francisco Navarrete

Reputation: 1

Firebase Database Function not working as it should on DialogFlow

I'm writing a code for a chatbot and i'm having a problem with the database query on my firebase DB that I don't know how to fix, because it is the same as the other examples I found.

    var ref = admin.database().ref();
    var consultasRef = ref.child('consultas')

    agent.add('Test 2')

    consultasRef.on("value", function(snap){
        agent.add('Test 3')
        agent.add(snap.val());
    });

As said the function that should print the snap.(val). It's not starting as it should. The right syntax is the same as the one I use. "Test 2" is being printed, unlike "Test 3", which is inside the function

For those unfamiliar with Dialogflow, agent.add() is the same as console.log() on JS, but for Dialogflow.

Upvotes: 0

Views: 79

Answers (1)

Prisoner
Prisoner

Reputation: 50701

When you make an asynchronous call (such as a database call), you must return a Promise from your Intent Handler so it knows when the call has completed so it can send the result back to the bot.

Additionally, you probably want to use the once() function, since you don't care about the database updating (since the result will already have been sent).

You can probably do both of these with code looking something like this:

return consultasRef.once('value')
  .then( snap => {
    agent.add('Test 3');
    agent.add(snap.val());
  });

There may also be issues with adding more than one or two text replies, depending on the Integration you're using.

Upvotes: 1

Related Questions