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`
checkIfFirstTimeis a very serious case of arrow head code, you should work on avoid writing code like that. Flattening such arrows makes the code much more maintainable.