1

I am new at Angular and using Firebase. When I check user login my function always return 0 even I did correct checking. My problem is my return value comes from outside.

 checkLog(mail: string, password: string): number {
        let isTrue = 0;
        this.http.get(exampleJson).subscribe(resData => {
            for (let data in resData) {
                this.http.get<User_model>(exampleJson' + data + '/.json').subscribe(res2Data => {
                    console.log('Name = ', res2Data.user_name, 'Password = ', res2Data.password);
                    if (mail === res2Data.user_name && password === res2Data.password) {
                        isTrue = 1;
                    }
                })
            }
        });
        return isTrue;
    }
Patrick
  • 15
  • 3

1 Answers1

1

The main reason is that you are making several asynchronous HTTP calls in your function, which take time to process.

When checkLog is called the workflow is the following :

  • let isTrue = 0; is called
  • this.http is called asynchronously which means it will take some time to process
  • return isTrue; is called right after

Your variable isTrue has no time to be processed and obviously return 0 because your HTTP calls did not process your variable in time.

One of the solution for your problem can be found on this topic.

Remi F
  • 26
  • 1