【问题标题】:How to Retrieve multiple data using specific field in Firebase Database如何使用 Firebase 数据库中的特定字段检索多个数据
【发布时间】:2019-08-23 10:49:26
【问题描述】:

我正在尝试按照 firebase 教程检索数据并在 Google 助手中显示。但我无法在 Dialogflow 实现中从数据库中检索多个数据。我想要求用户在该字段中输入注册 ID,其余字段的学生详细信息已获取。

我尝试了 firebase 文档。我的数据库连接成功,但我无法检索数据,并且我想要求用户输入学生 ID,即注册号。假设如果我输入 191611238 [RegId] 它将检索 FirstName、EmailId 和 year 字段。

* 这是我的 Dialogflow 实现代码 *

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);
    });

我想获取学生的详细信息。但我正在获取 Null,即 学生邮箱为空

学生注册号为空等 我在 Firebase 控制台中遇到错误

对话框流FirebaseFulfillment FIREBASE 警告:使用未指定的索引。考虑将 /Table 中的 ".indexOn": "RegId" 添加到您的安全规则中以获得更好的性能

请告诉我如何根据我要检索所有字段来要求用户输入 RegId。

【问题讨论】:

    标签: javascript firebase-realtime-database dialogflow-es-fulfillment


    【解决方案1】:
    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);
    
        }); 
      });                                                                                  
    

    【讨论】:

    • 这段代码运行良好..得到 Firebase 团队的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多