0

I have database structure is like this screenshot:

I had used orderByChild() to retrieve data from firebase Database using my Input as RegId, But I'm getting the values as NULL.

Please tell me how to retrieve data using RegId as Input from user.

return admin.database().ref('Table/'+RegId).orderByChild('RegId').once("value").then((snapshot) => {
    var name = snapshot.child("FirstName").val();
    agent.add(`The student name is ` + name);
    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);
})

I got output as:

The student name is null

The student Mail is null

The student Register no is null

ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25
ChinnapaReddy
  • 69
  • 1
  • 1
  • 8

1 Answers1

0

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

This means that your code will need to handle this list by looping over snapshot.forEach() in the callback:

return admin.database().ref('Table/'+RegId).orderByChild('RegId').once("value").then((snapshot) => {
  snapshot.forEach((user) => {
    var name = user.child("FirstName").val();
    agent.add(`The student name is ` + name);
    var email = user.child("EmailId").val();
    agent.add(`The student Mail is ` + email);
    var Regno = user.child("RegId").val();
    agent.add(`The student Register no is ` + Regno);
  });
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Following code is working But agent.add() function is not working i.e not getting output value to user.`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); 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);` }); – ChinnapaReddy Aug 26 '19 at 14:47
  • agent.add() is not working,But console.log() is giving values in Firebase Database logs.But i need agent.add() to show output values to user – ChinnapaReddy Aug 26 '19 at 14:51
  • The only change I made in my answer is to add a `snapshot.forEach(...)` loop, which is needed for the reasons I explained. There might be other problems with your code too, but this is definitely one thing you'll need to change. – Frank van Puffelen Aug 26 '19 at 15:17