Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Ionic 2 - this keyword is undefined when promise is returned from data service

$
0
0

@william_harris wrote:

I have a method in my data service which selects records from SqlStorage and returns the promise object...

import {Storage, SqlStorage} from 'ionic-angular';
import {Injectable} from 'angular2/core';
import {Journey} from '../../models/journey-model';

@Injectable()
export class Data {
    db: any;

    constructor() {
        this.db = new Storage(SqlStorage, { name: 'autoBusinessMiles.js' });
    }

    getJourneys() {
        let queryString = 'SELECT * FROM journey ORDER BY endTime DESC';
        return this.db.query(queryString);
    }
}

And I have a view which uses the data service...

import {Page, NavController} from 'ionic-angular';
import {Data} from '../../providers/data/data';

@Page({
    templateUrl: 'build/pages/journey-history/journey-history.html',
})
export class JourneyHistoryPage {
    isLoading: boolean;
    journeyList = [];
    constructor(public nav: NavController, private dataService: Data) {
        this.nav = nav;
        this.dataService = dataService;
    }

    getJourneys() {
        this.isLoading = true;
        this.dataService.getJourneys().then((response) => {
            if (response.res.rows.length > 0) {
                for (let journey of response.res.rows)  this.journeyList.push(journey);
            }
            this.isLoading = false;
        }, (err) => {
            this.isLoading = false;
        });
    }

    refreshList(refresher) {
        setTimeout(() => { refresher.complete(); }, 1000);
    }

    onPageWillEnter() {
        this.getJourneys();
    }
}

my problem is that when I loop through the data returned by my data service the this keyword is undefined but it should still be the JourneyHistoryPage am I right? so the variables on the page won't update.

Can anybody tell me if I have done something wrong here?

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles