首页 文章

承诺离子/角度 - 用户连接

提问于
浏览
0

我正在尝试使用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);
      });
  }

1 回答

  • 2

    不要将 options 字符串化,因为您必须以对象格式将数据发布到您的登录功能,如果您将 options 字符串化,则表示您正在发布字符串 . 因此,您无法从credentials.email和credentials.password访问登录功能中的数据

相关问题