Can't view anything inside promise resolution


#1

I have a method which returns a promise like:

checkLogin(credentials) {
        return new Promise((resolve, reject) => {
            this.http.post(url, credentials)
                .map(res => res.json())
                .subscribe(
                data => {
                    resolve(data);
                },
                err => {
                    reject(err);
                }
            );
        }); 
    }

I call this method inside another:

login(credentials) {
        this.checkLogin(credentials)
            .then(function(result) {
                console.log("ok: ",result);
                this.doAlert("ok");
            })
            .catch(function(err) {
                console.log("error: ",err.message);
                this.doAlert(err.message)
            });
}

Here is where the error happens, it is said “TypeError: this.doAlert is not a function”:

But the doAlert is in the same file than the others, and it works fine from other places (not promises calls)

   doAlert(text) {
        let alert = Alert.create({
            title: 'Alert;,
            subTitle: text,
            buttons: ['Ok']
        });
        this.nav.present(alert);
    }

Is it not possible to do this?


#2

[EDIT]
Use the Josh solution.

Try this:

login(credentials) {
    var self = this;
    this.checkLogin(credentials)
        .then(function(result) {
            console.log("ok: ",result);
            self.doAlert("ok");
        })
        .catch(function(err) {
            console.log("error: ",err.message);
            self.doAlert(err.message)
        });
}

#3

Do this instead:

login(credentials){
	this.checkLogin(credentials).then((result) => {
            console.log("ok: ",result);
            this.doAlert("ok");
	}, (err) => {
	    console.log("error: ",err.message);
	    this.doAlert(err.message)   	
	});
}

#4

this works, thank you so much