0

Hello i am trying to write a function that find all username below userid and decide the username already exists or not

my json looks like this

    {
  "ali": {
    "AccessToken": "asdadas",
    "CreatedAt": "2017/12/09",
    "DeletedAt": -1,
    "Email": "sadadada",
    "FirstName": "asdad",
    "LastName": "asdadad",
    "LoginType": 0,
    "Phone": "",
    "ThumbNailUrl": "",
    "UpdatedAt": -1,
    "UserName": "asdada"
  },
  "asdadad": {
    "AccessToken": "asdadas",
    "CreatedAt": "2017/12/09",
    "DeletedAt": -1,
    "Email": "asdadadada",
    "FirstName": "asdada",
    "LastName": "asdada",
    "LoginType": 0,
    "Phone": "",
    "ThumbNailUrl": "",
    "UpdatedAt": -1,
    "UserName": "asdadadada"
  }
}

my functions looks like this

    exports.CheckUserName = functions.https.onRequest((req,res) => {
    const userName = req.query.userName;
    if (userName == "" || userName == null) {
        return res.json({
            result: "UserName can not be null or empty"
        })
    } else {
        // what i have to do in here :(
    }
})

i tried a lot of way but i couldn't do it

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ALKIN ÇAKIRALAR
  • 237
  • 2
  • 14

1 Answers1

0

I'm java developer so i'll tell you the logic. you can implement in your javascript

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("userId");

rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  void onDataChange(DataSnapshot snapshot) {
    if (snapshot.hasChild("your username")) {
      // run some code
    }
  }
});

Now you can find all username under id. If exists print something according to your need

If you don't know id you can save all usernames in separate node called usernames and put all usernames in there and always check from there if usernames exists in that node or not. pretty simple :)

Muhammad Saad
  • 713
  • 1
  • 9
  • 31