简体   繁体   English

等待函数在 if 中完成

[英]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.我正在尝试进行测试,但它不会等待在数据库中发出请求,并且看起来该函数返回未定义,并且仅在请求之后它返回 true 或 false 并且测试落在 else 上。

My problem is in RestService of angular 4/5我的问题是在 4/5 角的 RestService

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:为了等待异步操作,您必须返回this.rest.post创建的 observable:

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

And then, subscribe to this observable in data :然后,在data订阅这个 observable :

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 .注意:如果还需要等待data外的put动作,则不能订阅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并订阅data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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