Firebase Promise error handling from the component?


#1

Hello everybody,
still learning and rocking in NativeScript!

This is my registerUser routine, inside a login.service.ts service:

    registerBuyer (_b: Buyer) {
        return firebase.createUser({
            email: _b.email,
            password: _b.password,
        }).then(
            (result: any) => {
                _b.uid = result.key;
                firebase.setValue('/buyers/' + result.key, {
                    name: _b.name,
                    surname: _b.surname,
                    company: _b.company
                })
            },
            (errorMessage: any) => {
                console.log("Err: " + errorMessage);
            }
        );
    }

In my RegisterComponent I’m doing:

signUp () {
        this.loginService.registerBuyer(this._newBuyer).then(() => {
            this.loginService.login(this._newBuyer.email, this._newBuyer.password).then(() => {
                this.router.navigate(['/home/categories'], {clearHistory: true});
            });
        }).catch(
            (error) => alert(error)
        );
    }

If I try to sign up with an existing e-mail address, Firebase returns an error that is correctly console-logged in the service layer, but the catch() callback is never entered in the Component. Instead, the then() callback is always executed and I get routed to the home which is not what I want.

Am I missing some key Promise concept?

Thanks


#2

We’re doing something very similar in our app and this is what ended up working for us:

registerBuyer(_b: Buyer): Promise<any> {
         return new Promise<any>((resolve, reject) => {
             firebase.createUser({
                 email: _b.email,
                 password: _b.password,
             }).then(
                 (result: any) => {
                     _b.uid = result.key;
                     firebase.setValue('/buyers/' + result.key, {
                         name: _b.name,
                         surname: _b.surname,
                         company: _b.company
                     })
                         .then(() => resolve(result))
                         .catch((err) => reject(err));
                 })
                 .catch((errorMessage) => {
                     console.log("Err: " + errorMessage);
                     reject(errorMessage);
                 });
         });
     }