0

I am new to node js and developing chat bot using Dialogflow. To get specific response from firebase, I have to call functions as below.

  admin.database().ref(institutes+'/'+ Programme).once("value").then((snapshot) => {
        reply= snapshot.val();}).then((reply) =>                                        {
        for (var key in reply)
        {
             console.log('1 executed');
             admin.database().ref(institutes+'/'+ Programme +'/' + key + '/' + 'Group').once("value").then((datagroup)=>
             {
                 if(datagroup.val() == Group)
                 {
                     console.log('2 executed');
                     result+=key;
                 }
             });
        }
  });
  agent.add(`Hello`);

Code run successfully with hello message in reply but It is not executing function written inside for loop. i.e. in console there is no messages like 1 executed or 2 executed.

Bill P
  • 3,622
  • 10
  • 20
  • 32
  • The question in the current state does not provide adequate information to enable understanding of what the problem may be. Have you checked the logs @ https://console.firebase.google.com/u/0/project/your-project-name/functions/list? The problem definition should be in the logs. – Terungwa Oct 11 '19 at 13:20
  • To use the .once() method to read the data located at your reference, you need to confirm that the child location you have referenced above `(institutes+'/'+ Programme)`exists in your Database. – Terungwa Oct 11 '19 at 13:31

1 Answers1

-1

I recommed you use this example code for contruct your code:

function readFromDb (agent) {
    // Get the database collection 'dialogflow' and document 'agent'
    const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

    // Get the value of 'entry' in the document and send it to the user
    return dialogflowAgentDoc.get().then(doc => {
        if (!doc.exists) {
            agent.add('No data found in the database!');
        } else {
            agent.add(doc.data().entry);
        }

        return Promise.resolve('Read complete');
    }).catch(() => {
        agent.add('Error reading entry from the Firestore database.');
        agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
    });
}

Code taken from this github file in the dialogflow/fulfillment-firestore-nodejs repo

I used this to make my node.js code for a deploy on firebase. I don't use a for in that but in mi case I need to use a if and this work perfectly.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/24863184) – Jason Aller Dec 15 '19 at 23:07
  • Thanks for your review, I will have this in mind when I answer another post. – mcblue30 Dec 17 '19 at 06:03