简体   繁体   中英

Wait function finish in if

I'm trying to do a test but it does not wait to make the request in the database and it looks like the function returns undefined and only after the request it returns true or false and the test falls on the else.

My problem is in RestService of angular 4/5

function in which you perform the password validation to perform an update

    handler: data => {
      if (this.checkPassword(data.password)) {
        console.log("4--------");
        // logged in!
        let jsonCreate = {
            name: form.value.inputName,
            email: form.value.inputEmail,
            password: form.value.inputPassword,
            description: form.value.inputDescription,
            id: this.id
        };
        console.log('jsonCreate');
        console.log(jsonCreate);
        //adiciona cliente ao banco
        this.rest.put('usersClient/atualizacao', jsonCreate)
        .subscribe(userUpdated => {
            console.log(userUpdated);

        });
      } else {
        console.log("3--------");
        return false;
      }
    }

check password func

  }
  public checkPassword(password){
      console.log("check");

    let jsonPost = {};
    jsonPost["email"] = this.user.email;
    jsonPost["senha"] = password;
    console.log(jsonPost);
    this.rest.post('Login/clientAuth', jsonPost).subscribe(user => {
        console.log("11--------");
        if(this.isEmptyObject(user)==true){
            console.log("1--------");
          return false;
        }
        else{
            console.log("2--------");
            return true;

        }
    });
  }

In order to wait for the async action, you must return the observable that this.rest.post creates:

public checkPassword(password){
    ...
    return this.rest.post('Login/clientAuth', "").map(user => !this.isEmptyObject(user));
});

And then, subscribe to this observable in data :

handler: data => {
    this.checkPassword(data.password).subscribe(isLoggedIn => {
        if (isLoggedIn) {
            let jsonCreate = {
                ...
            };

            this.rest.put('usersClient/atualizacao', jsonCreate).subscribe(userUpdated => {
                console.log(userUpdated);
            });
        } else {
            return false;
        }
    });
}

Note: if you also need to wait for the put action outside data , you cannot subscribe to checkPassword . You need to do something like this:

handler: data => {
    this.checkPassword(data.password).flatMap(isLoggedIn => {
        if (isLoggedIn) {
            let jsonCreate = {
                ...
            };
            return this.rest.put('usersClient/atualizacao', jsonCreate).map(userUpdated => {
                console.log(userUpdated);
            });
        } else {
            return false;
        }
    });
}

And subscribe to data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM