0

I tried subscribing to an observable returning an array, the array object print to console quite well, but accessing the content of the array raise undefined error and array.length print out 0;

The observable generating fetching the return array from datastore

getDataFromDatabase(sqlStatement: string, querydata?: any[]): Observable<any> {
    let datas: Array<any> = [];
    this.database.executeSql(sqlStatement, querydata ? querydata : []).then((data) => {
        let len = data.rows.length;
        console.log('database data.rows', data.rows);
        for (let i = 0; i < len; i++) {
            let d = data.rows.item(i);
            datas.push(d);
        }
    },
        (err) => {
            datas = err;
        }
    );

    return observable.forkJoin(observable.of(datas));
}

The function subscribing to the observable

checkIfFirstTime(): Observable<number> {
    this.database.getDatabaseState().subscribe(state => {
        if (state) {
            let query = "SELECT appLaunch AS al FROM User WHERE id = ?";
            this.database.getDataFromDatabase(query, [1]).subscribe(data => {
                if (data != null) {
                    data = data[0];
                    console.log(data.length, ' - Check-auth.service 1...', data);
                    if (data[0].al) {
                        let query2 = "SELECT pin AS pin FROM Setting WHERE id = ?";
                        console.log(query2);
                        this.database.getDataFromDatabase(query2, [200]).subscribe(data2 => {
                            data2 = data2[0];
                            if (data2[0].pin && this.login) {
                                this.authIndex = 1;
                            }
                            else if (data2[0].pin && (!this.login)) {
                                this.authIndex = 3;
                            }
                            else if (!data2[0].pin) {
                                this.authIndex = 1;
                            }
                        });
                    }
                    else if (!data[0].al) {
                        console.log('hey am here');
                        this.authIndex = 2;
                    }
                }
            });
        }
    });

    return observer.of(this.authIndex);
}

Error result

Array(1)0: {al: 0}length: 1__proto__: Array(0) "Check-auth.service 1..." Array(1)0: [{…}]length: 1__proto__: Array(0) "....3" 1 vendor.js:55437 ERROR TypeError: Cannot read property 'al' of undefined at SafeSubscriber._next (default~home-home-module~pages-calendar-calendar-module~pages-pin-link-pin-link-module~pages-reminde~b3c38b73.js:9057) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (vendor.js:94208) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (vendor.js:94146) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (vendor.js:94089) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (vendor.js:94066) at ForkJoinSubscriber.push../node_modules/rxjs/_esm5/internal/observable/forkJoin.js.ForkJoinSubscriber.notifyComplete (vendor.js:95234) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._complete (vendor.js:93292) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.complete (vendor.js:94078) at vendor.js:104694 at subscribeToResult (vendor.js:104875) defaultErrorLogger @ vendor.js:55437 main.js:3124 database data.rows Object`

1

1 Answer 1

1
 this.database.getDataFromDatabase(query, [1]).subscribe(data=>{
                if(data!=null){
                    data = data[0];

You are overriding the data that you get and is no longer an array, so it can't get 'al' at pos[0].

Try to use different and more descriptive names to your variables to not get confused.

Sign up to request clarification or add additional context in comments.

1 Comment

the forkjoin() function create another array making the array a 2x2 array the original data is data[0][0].al so i decide to to pass data to data[0] to data;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.