1

This is my firebase data

users
 -L2yfvAU0cihwcJHqIv9
     City: "wah cantt"
     Email: "[email protected]"
     Firstname: "sharjeel"
     Lastname: "malik"
     Latitude: "74.27559833333333"
     Longtitude: "31.509099999999997"
     Phone: "03035602137"
     Username: "sharjeel089"

This is my current cloud function

exports.retreivefromdatabase = functions.https.onRequest((req,res) => {
var db = admin.database();
var ref = db.ref();

ref.on("value", function(snapshot){
res.send(snapshot.val());
});
});

This function retrieves all data from firebase database. I just want to retrieve user data in the form of key value pairs like this on the basis of username.

 {"City": "wah 
cantt","Email":"[email protected]","Firstname":"sharjeel"........}

How to do that?

1
  • If you are new to JavaScript or accessing the Firebase Database from it, Cloud Functions for Firebase is not the best way to learn it. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. After those you'll be much better equipped to write code for Cloud Functions too. Commented Jan 16, 2018 at 15:39

1 Answer 1

2

This is one of the many ways to do this:

exports.retreivefromdatabase = functions.https.onRequest((req,res) => {
  var db = admin.database();
  var ref = db.ref();
  var username = req.params.username;

  ref.orderByChild("Username").equalTo(username).once("child_added", function(snapshot){
    res.send(snapshot.val());
  });
});

As I commented, Cloud Functions is not the easiest nor best way to learn how to interact with the Firebase Database from JavaScript. I'd recommend taking the codelab I mentioned or one of the many other tutorials out there. These allow you to simply run your code in a web page, which is a way faster way to work this out. Then once you've gotten the basics, deploying similar functionality on Cloud Functions is a much simpler step.

Sign up to request clarification or add additional context in comments.

1 Comment

How to add parameters in my http request? "Params" means parameters right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.