如何使用 Firebase 数据库中的特定字段检索多个数据
How to Retrieve multiple data using specific field in Firebase Database
我正在尝试按照 firebase 教程检索数据并在 Google 中显示 assistant.But 我无法从 Dialogflow 中的数据库检索多个数据 fulfillment.I 想询问用户要在该字段中输入注册 ID,已获取学生详细信息的其余字段。
我尝试了 firebase 文档。我的数据库连接成功,但我无法检索数据,而且我想要求用户输入学生 ID,即注册号。假设如果我输入 191611238 [RegId] 它将检索 FirstName、EmailId 和年份字段。
* 这是我的 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);
});
我想获取 student.but 的详细信息,我正在获取 Null 即
学生邮箱为空
学号为空等
我在 Firebase 控制台中收到错误
dialogflowFirebaseFulfillment
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "RegId" at /Table to your security rules for better performance
请告诉我如何根据我要检索所有字段要求用户输入 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);
});
});
我正在尝试按照 firebase 教程检索数据并在 Google 中显示 assistant.But 我无法从 Dialogflow 中的数据库检索多个数据 fulfillment.I 想询问用户要在该字段中输入注册 ID,已获取学生详细信息的其余字段。
我尝试了 firebase 文档。我的数据库连接成功,但我无法检索数据,而且我想要求用户输入学生 ID,即注册号。假设如果我输入 191611238 [RegId] 它将检索 FirstName、EmailId 和年份字段。
* 这是我的 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);
});
我想获取 student.but 的详细信息,我正在获取 Null 即 学生邮箱为空
学号为空等 我在 Firebase 控制台中收到错误
dialogflowFirebaseFulfillment FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "RegId" at /Table to your security rules for better performance
请告诉我如何根据我要检索所有字段要求用户输入 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);
});
});