Ionic Observable returning immediatly


#1

Hi all.

I’m having a bit of an issue with an Observable seen below:

  private ConfirmAuthenticationAvailable()
  {
    if (this.registerCredentials.logID === null || this.registerCredentials.password === null) {
      return Observable.throw("Please insert credentials");
    } else {
      console.log("got to here")
      return Observable.create(observer => {
      console.log("got to can't get to here")

        this.sqlite.create({
          name: 'ionicdb.db',
          location: 'default'
        }).then((db: SQLiteObject) => {

          db.executeSql('SELECT * FROM users;', {})
            .then((resUsersLoaded) => {
              this.userSelection = resUsersLoaded;
              for (let i = 0; i < resUsersLoaded.rows.length; i++) {
                let item = resUsersLoaded.rows.item(i);
                console.log(item);
              }
          }).catch(e => console.log(e));

        }
        ).catch(e => console.log(e));

       // this.currentUser = new User('ename', 'ename@email.ca');
        observer.next(access);
        observer.complete();
      });

    }
  }

In debugging the code (and watching for the console outputs) the code appears to return immediatly at:
return Observable.create(observer => {

I’m I structuring this in an inappropriate way that would cause this to happen? I’m coming up short looking at the documentation and the source I’m working from.

(sorry for the ugly code otherwise, it’s part of a WIP…)


#2

I can’t speak to “inappropriate”, but I find this both really hard to read and an exercise in futility. You can’t do authentication on device; it must be done on a server that you control. That sort of moots the rest of the discussion, but it is really rare that you would need or want to be creating Observables manually.


#3

Fair enough. it’s just a placeholder for now until I get to that stage. I’ll have to do more reading - thanks!


#4

Unfortunately that choice of placeholder is forcing you to deal with impedance mismatch between Promises and Observables that you won’t have to worry about once you do get to the stage of doing this over HTTP, because HttpClient is already going to be giving you an Observable to work with.