Search code examples
javascriptfirebase-realtime-databasedialogflow-es-fulfillment

How to Retrieve multiple data using specific field in Firebase Database


I am trying to follow the firebase tutorial to retrieve data and display in Google assistant.But I am not able to retrieve multiple data from database in Dialogflow fulfillment.I want to ask user to enter register id with that field, remaining fields of student details were fetched.

I tried firebase documentation. My database connection was successful,But I am not able to retrieve data and also I want to ask user to enter the student Id i.e Register number. Suppose if i enter 191611238 [RegId] It will retrieve FirstName,EmailId and year fields.

* This is my Dialogflow fulfillment code *

const functions = require('firebase-functions');
const { WebhookClient} = require('dialogflow-fulfillment');


// initialise DB connection
const admin = require('firebase-admin');
admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: 'ws://******.firebaseio.com/'
});

process.env.DEBUG = 'dialogflow:debug';

exports.dialogflowFirebaseFulfillment =
    functions.https.onRequest((request, response) => {

        const agent = new WebhookClient({
            request,
            response
        });
        console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
        console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

        function getRegId(agent) {
            const RegId = agent.parameters.RegId;

            agent.add(`Thank you...`);


            return
            admin.database().ref('Table/').orderByChild('/RegId').equalTo("$RegId").once("value").then((snapshot) => {

                var Email = snapshot.child("EmailId").val();
                agent.add(`The student Mail is ` + Email);
                var Regno = snapshot.child("RegId").val();
                agent.add(`The student Register no is ` + Regno);
                var name = snapshot.child("FirstName").val();
                agent.add(`The student name is ` + name);
                var year = snapshot.child("CourseName").val();
                agent.add(`The student currently studying ` + year);
                var Gradu = snapshot.child("GraduationTypeName").val();
                agent.add(`The student Department is ` + Gradu);

            });
        }

        // Run the proper function handler based on the matched Dialogflow 
        intent name
        let intentMap = new Map();
        intentMap.set('GetthedetailsofRegisternumber', getRegId);
        agent.handleRequest(intentMap);
    });

I want to get the details of student.but Iam getting Null i.e The student Mail is null

The student Register no is null etc I got error in Firebase console as

dialogflowFirebaseFulfillment FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "RegId" at /Table to your security rules for better performance

Please provide me how to ask the user to enter RegId based on that I want to retrieve all fields.

This is my databse Structure


Solution

  • function handleGetthedetailsofRegisternumber(agent){
    
      const RegId = agent.parameters.RegId;
    
        var ref = admin.database().ref().child("Table/");
    var query = ref.orderByChild("RegId").equalTo(RegId.toString());
    
    query.once("value", function(snapshot) {
      snapshot.forEach(function(child) {
      console.log(child.key);
          // name field
    
      console.log("FirstName: " + child.val().FirstName);
      console.log("Mobile: " + child.val().MobileNumber);
      console.log("Email: " + child.val().EmailId);
      var name = snapshot.child("FirstName").val();
      agent.add(`The student name is ` + name);
    
        }); 
      });