簡體   English   中英

如何使用 Firebase 數據庫中的特定字段檢索多個數據

[英]How to Retrieve multiple data using specific field in Firebase Database

我正在嘗試按照 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);
    });

我想得到學生的詳細信息。但我得到了空即學生郵件為空

學生注冊號為空等我在 Firebase 控制台中出錯

dialogflowFirebaseFulfillment FIREBASE 警告:使用未指定的索引。 考慮將 /Table 中的 ".indexOn": "RegId" 添加到您的安全規則中以獲得更好的性能

請告訴我如何根據我要檢索的所有字段要求用戶輸入 RegId。

這是我的數據庫結構

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

    }); 
  });                                                                                  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM