Read data from firebase and display in chat using Node Js












0















I am new to the Firebase realtime database and Google Dialogflow. I have gone through the documents and working on through. I am reading the data from database and I want to display it in my chat. I am able to see the data in the logs but unable to display in the chat conversation. If I check the logs I am able to see the success or failed result but unable to view in the chat conversation.



This is the code below:



var childData = "";    
var message = '';
var query = '';
var key = '';

function wheretogo(agent) {
//taking country name as input from user
var country = request.body.queryResult.parameters.country;
//reference country from the database
query = admin.database().ref("country").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
key = childSnapshot.key;
childData = childSnapshot.val();
//matching the input from user and the country name(key) from database
if (country === key) {
console.log("sucess");
message = 'Thats nice ! You are travelling to ' + key;
agent.add(message);
}
else
{
console.log("failed");
}
});
});
}


I expect the output 'Thats nice ! You are travelling to ' with country name in my chat conversation.










share|improve this question





























    0















    I am new to the Firebase realtime database and Google Dialogflow. I have gone through the documents and working on through. I am reading the data from database and I want to display it in my chat. I am able to see the data in the logs but unable to display in the chat conversation. If I check the logs I am able to see the success or failed result but unable to view in the chat conversation.



    This is the code below:



    var childData = "";    
    var message = '';
    var query = '';
    var key = '';

    function wheretogo(agent) {
    //taking country name as input from user
    var country = request.body.queryResult.parameters.country;
    //reference country from the database
    query = admin.database().ref("country").orderByKey();
    query.once("value")
    .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
    key = childSnapshot.key;
    childData = childSnapshot.val();
    //matching the input from user and the country name(key) from database
    if (country === key) {
    console.log("sucess");
    message = 'Thats nice ! You are travelling to ' + key;
    agent.add(message);
    }
    else
    {
    console.log("failed");
    }
    });
    });
    }


    I expect the output 'Thats nice ! You are travelling to ' with country name in my chat conversation.










    share|improve this question



























      0












      0








      0


      1






      I am new to the Firebase realtime database and Google Dialogflow. I have gone through the documents and working on through. I am reading the data from database and I want to display it in my chat. I am able to see the data in the logs but unable to display in the chat conversation. If I check the logs I am able to see the success or failed result but unable to view in the chat conversation.



      This is the code below:



      var childData = "";    
      var message = '';
      var query = '';
      var key = '';

      function wheretogo(agent) {
      //taking country name as input from user
      var country = request.body.queryResult.parameters.country;
      //reference country from the database
      query = admin.database().ref("country").orderByKey();
      query.once("value")
      .then(function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
      key = childSnapshot.key;
      childData = childSnapshot.val();
      //matching the input from user and the country name(key) from database
      if (country === key) {
      console.log("sucess");
      message = 'Thats nice ! You are travelling to ' + key;
      agent.add(message);
      }
      else
      {
      console.log("failed");
      }
      });
      });
      }


      I expect the output 'Thats nice ! You are travelling to ' with country name in my chat conversation.










      share|improve this question
















      I am new to the Firebase realtime database and Google Dialogflow. I have gone through the documents and working on through. I am reading the data from database and I want to display it in my chat. I am able to see the data in the logs but unable to display in the chat conversation. If I check the logs I am able to see the success or failed result but unable to view in the chat conversation.



      This is the code below:



      var childData = "";    
      var message = '';
      var query = '';
      var key = '';

      function wheretogo(agent) {
      //taking country name as input from user
      var country = request.body.queryResult.parameters.country;
      //reference country from the database
      query = admin.database().ref("country").orderByKey();
      query.once("value")
      .then(function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
      key = childSnapshot.key;
      childData = childSnapshot.val();
      //matching the input from user and the country name(key) from database
      if (country === key) {
      console.log("sucess");
      message = 'Thats nice ! You are travelling to ' + key;
      agent.add(message);
      }
      else
      {
      console.log("failed");
      }
      });
      });
      }


      I expect the output 'Thats nice ! You are travelling to ' with country name in my chat conversation.







      firebase firebase-realtime-database dialogflow






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 18:50







      slashPriya

















      asked Jan 1 at 10:36









      slashPriyaslashPriya

      12




      12
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The problem is that the Dialogflow library expects you to return a Promise if you're doing any asynchronous calls. Since you're doing an async call (the query.once() call), you must return a Promise. Otherwise the handler dispatcher won't wait for the reply to come from the database before it tries to send a reply to the user.



          You don't show all of your code, but in your case it looks fairly straightforward. Since query.once() returns a Promise, you can return this Promise. Something like this change



           return query.once("value") 


          might be all that is necessary.






          share|improve this answer
























          • thank you so much for the answer @Prisoner. It worked!!!!

            – slashPriya
            Jan 1 at 18:20













          • Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

            – Prisoner
            Jan 2 at 0:04











          • Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

            – slashPriya
            Jan 2 at 5:06











          • You can always accept an answer to a question you posted.

            – Prisoner
            Jan 2 at 11:53











          • query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

            – slashPriya
            Jan 18 at 14:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994777%2fread-data-from-firebase-and-display-in-chat-using-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The problem is that the Dialogflow library expects you to return a Promise if you're doing any asynchronous calls. Since you're doing an async call (the query.once() call), you must return a Promise. Otherwise the handler dispatcher won't wait for the reply to come from the database before it tries to send a reply to the user.



          You don't show all of your code, but in your case it looks fairly straightforward. Since query.once() returns a Promise, you can return this Promise. Something like this change



           return query.once("value") 


          might be all that is necessary.






          share|improve this answer
























          • thank you so much for the answer @Prisoner. It worked!!!!

            – slashPriya
            Jan 1 at 18:20













          • Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

            – Prisoner
            Jan 2 at 0:04











          • Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

            – slashPriya
            Jan 2 at 5:06











          • You can always accept an answer to a question you posted.

            – Prisoner
            Jan 2 at 11:53











          • query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

            – slashPriya
            Jan 18 at 14:28
















          0














          The problem is that the Dialogflow library expects you to return a Promise if you're doing any asynchronous calls. Since you're doing an async call (the query.once() call), you must return a Promise. Otherwise the handler dispatcher won't wait for the reply to come from the database before it tries to send a reply to the user.



          You don't show all of your code, but in your case it looks fairly straightforward. Since query.once() returns a Promise, you can return this Promise. Something like this change



           return query.once("value") 


          might be all that is necessary.






          share|improve this answer
























          • thank you so much for the answer @Prisoner. It worked!!!!

            – slashPriya
            Jan 1 at 18:20













          • Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

            – Prisoner
            Jan 2 at 0:04











          • Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

            – slashPriya
            Jan 2 at 5:06











          • You can always accept an answer to a question you posted.

            – Prisoner
            Jan 2 at 11:53











          • query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

            – slashPriya
            Jan 18 at 14:28














          0












          0








          0







          The problem is that the Dialogflow library expects you to return a Promise if you're doing any asynchronous calls. Since you're doing an async call (the query.once() call), you must return a Promise. Otherwise the handler dispatcher won't wait for the reply to come from the database before it tries to send a reply to the user.



          You don't show all of your code, but in your case it looks fairly straightforward. Since query.once() returns a Promise, you can return this Promise. Something like this change



           return query.once("value") 


          might be all that is necessary.






          share|improve this answer













          The problem is that the Dialogflow library expects you to return a Promise if you're doing any asynchronous calls. Since you're doing an async call (the query.once() call), you must return a Promise. Otherwise the handler dispatcher won't wait for the reply to come from the database before it tries to send a reply to the user.



          You don't show all of your code, but in your case it looks fairly straightforward. Since query.once() returns a Promise, you can return this Promise. Something like this change



           return query.once("value") 


          might be all that is necessary.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 12:03









          PrisonerPrisoner

          35.1k33258




          35.1k33258













          • thank you so much for the answer @Prisoner. It worked!!!!

            – slashPriya
            Jan 1 at 18:20













          • Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

            – Prisoner
            Jan 2 at 0:04











          • Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

            – slashPriya
            Jan 2 at 5:06











          • You can always accept an answer to a question you posted.

            – Prisoner
            Jan 2 at 11:53











          • query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

            – slashPriya
            Jan 18 at 14:28



















          • thank you so much for the answer @Prisoner. It worked!!!!

            – slashPriya
            Jan 1 at 18:20













          • Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

            – Prisoner
            Jan 2 at 0:04











          • Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

            – slashPriya
            Jan 2 at 5:06











          • You can always accept an answer to a question you posted.

            – Prisoner
            Jan 2 at 11:53











          • query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

            – slashPriya
            Jan 18 at 14:28

















          thank you so much for the answer @Prisoner. It worked!!!!

          – slashPriya
          Jan 1 at 18:20







          thank you so much for the answer @Prisoner. It worked!!!!

          – slashPriya
          Jan 1 at 18:20















          Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

          – Prisoner
          Jan 2 at 0:04





          Glad it works! If an answer has helped, accepting and/or upvoting it is always appreciated.

          – Prisoner
          Jan 2 at 0:04













          Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

          – slashPriya
          Jan 2 at 5:06





          Glad to up vote the answer @Prisoner but i have less reputation don't mind!!

          – slashPriya
          Jan 2 at 5:06













          You can always accept an answer to a question you posted.

          – Prisoner
          Jan 2 at 11:53





          You can always accept an answer to a question you posted.

          – Prisoner
          Jan 2 at 11:53













          query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

          – slashPriya
          Jan 18 at 14:28





          query = admin.database().ref('country/'+country+'/mobileEntitlement/').orderByKey(); traveldesk.firebaseio.com/country/Australia/… i have country named australia in the database and it is hitting that link correctly) But the problem is i dont have country japan in my database but still it is hitting the below link which is not present in the database traveldesk.firebaseio.com/country/japan/mobileEntitlement(here it has to throw error because in the link path japan is not present still it is hitting the database)

          – slashPriya
          Jan 18 at 14:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994777%2fread-data-from-firebase-and-display-in-chat-using-node-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas