angularjs - 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);
});
}
解决方案
不要对选项进行字符串化,因为您必须以对象格式将数据发布到登录函数,如果您对选项进行字符串化,则意味着您正在发布一个字符串。因此,您无法从 credentials.email 和 credentials.password 访问登录功能中的数据
推荐阅读
- javascript - 通过比较项目对 JavaScript 数组项目进行分组
- flutter - 如何在聊天应用中为应用级别的 mod 实现 Mod 工具
- php - 接收 cURL 错误:来自服务器的空回复
- reactjs - 如何使用 React/Next.js 将数据传递到模态?
- drupal-8 - Drupal 8 - 我在哪里将 interface.default 更改为 sparkpost_mail
- python - 在 Python 中创建分数
- r - 如何将 geom_text 和箱线图颜色与离散的 x 轴结合起来?
- file - Seafile 是真正的云存储吗?
- servicenow - 如何从 Service-Now 中的脚本调用其他应用程序 API
- django - 如何在 django admin 的另一个应用程序中将外键显示为指向模型的链接