How to get variable inside a method to global variable in ionic 2

Here is my code for login using firebase.I used foreach method in firebase documentation.
The thing is how i can return the key and child data to global values?
if i use cosole,log it will show the correct value but i can’t find a way to get the values out and pass it to next page.
Any suggestions??

Login(){
  
  let x = this.tyindexno;
  let y =this.typassword;
var query = this.userlistRef.orderByKey();
query.once("value",function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var key = childSnapshot.key;
      if(key==x){
      // Cancel enumeration
      console.log(key);
      var childData = childSnapshot.val();
      
      if(childData.password==y){
     console.log(childData.password);
      }
      return true;
    }   
  });
})
}

HOW TO POST CODE.

Unless you are absolutely sure of why you are doing so, never type the word ‘function’ in an Ionic app. Always use arrow functions, and things will behave more like you expect.

Thank you for the quick reply and actually i don’t know about that thing.Also sorry for the mistake in code posting.
Can you show me how to format my code to work??
or any suggestions to get data from firebase db and use it for loging?
all i need is get the data from firebase and use it for confirm the login.

here is my firebase data and IT15… are the index numbers

image

I suppose rapropos mentioned the use of arrow functions like this:

public login(): void {
  
  let x = this.tyindexno;
  let y = this.typassword;
  var query = this.userlistRef.orderByKey();
  query.once("value", (snapshot) => {
    snapshot.forEach((childSnapshot) => {
      var key = childSnapshot.key;
      if (key==x) {
        // Cancel enumeration
        console.log(key);
        var childData = childSnapshot.val();
      
        if (childData.password==y) {
          console.log(childData.password);
        }
      return true;
      }   
    });
  })
}

You should use appropriate variable names to help us understand the code.
For the rest, I do not understand your question. A global variable?
And why are you retrieving and comparing passwords? Are you sure that you are implementing a correct design? Why do not you use firebase auth to manage sessions?
There are very good tutorials going around on the internet that can help you do what you need.

1 Like

Thanx for the reply n if i use google auth is it possible to create and use firebase custom keys like i mentioned above???

I need to crete my own key for one student and use it as id

Yes you can.

You will need to create an object with bracket notation like this:

let userKey: string = 'IT1155236';

updates['/users/' + userkey] = {  
    oneParam: 'param1',
    otherParam: 'param2'
};

/**
 * To update an existing entry. If exist, it updates, else creates a new one.
 */
firebase.database().ref().update(updates);

In the docs it’s explained with examples :slight_smile:

THank you and after I do like this it is poassible to return the output as a boolean.
And I decide to use firebase auth too.
Then used this function to retrive specific data search.
Thnx again:slight_smile: