首页 > 解决方案 > Promise Ionic/Angular - 用户连接

问题描述

我正在尝试使用 Promise 来连接我的 Ionic App 上的用户。我尝试了很多东西......但这不起作用。有什么问题 ?你能帮我吗 ?

在验证“访问”之前,我必须执行我的加载功能。

这是我目前的代码。怎么了 ?(目前,错误是:“声明类型既不是 'void' 也不是 'any' 的函数必须返回一个值。”)

这是我的加载功能(没问题,这是我必须首先执行的)

load(credentials) 
   {

    let headers       : any     = new HttpHeaders({ 'Content-Type': 'application/json' }),
    options     : any       = { "pseudo" : credentials.email, "mdp" : credentials.password },
    url       : any   = "http://localhost:8888/authVerif2.php" ;


    return new Promise((resolve) => {

      this.http
      .post(url, JSON.stringify(options), headers)
      .subscribe(data => {
        console.dir(data);
        this.infosUser = data;
        //console.log(this.infosUser[0].prenom);
      resolve(this.infosUser);

    },


    (error : any) =>
    {
      console.dir(error);
    });

   })
  }

这是我的登录功能:

public login(credentials) : Observable<any>{
      this.load(credentials)
      .then( a =>
        {


          if (credentials.email === null || credentials.password === null) {
            return Observable.throw("Please insert credentials");
          } else {
            return Observable.create(observer => {
              // At this point make a request to your backend to make a real check!      


              let access = (credentials.password === "pass" && credentials.email === "email");
              this.currentUser = new User('Simon', 'saimon@devdactic.com');
              observer.next(access);
              observer.complete();


            });
          }
        }
        );
  }

这两个功能位于名为“auth-service”的提供程序中。此提供程序在我的页面“login.html”中使用

public login() {
    this.showLoading()
    this.auth.login(this.registerCredentials).subscribe(allowed => {
      if (allowed) {        
        this.nav.setRoot(HomePage);
      } else {
        this.showError("Access Denied");
      }
    },
      error => {
        this.showError(error);
      });
  }

标签: angularjsionic-frameworkionic3angular-promise

解决方案


不要对选项进行字符串化,因为您必须以对象格式将数据发布到登录函数,如果您对选项进行字符串化,则意味着您正在发布一个字符串。因此,您无法从 credentials.email 和 credentials.password 访问登录功能中的数据


推荐阅读