我正在尝试登录用户。
我正在调用以下API来检查凭据:
http://5c55225d84df580014cd06a3.mockapi.io/users?search=Ally45
即使我对API的调用似乎返回了正确的数据,但由于某种原因,我的代码也无法识别返回的数据。
在自己的登录-page.xml,我打电话的登录()函数当用户点击登录按钮:
login-view-model.js
// some code omitted for brevity...
function LoginViewModel() {
const viewModel = observableModule.fromObject({
processing: false,
email: "Ally45",
password: "12345",
login() {
this.processing = true;
userService.login({
email: this.email,
password: this.password
}).then(() => {
this.processing = false;
topmost().navigate("./home/home-page");
})
.catch((e) => {
this.processing = false;
alert("Unfortunately we could not find your account.");
});
},
});
return viewModel;
}
所以我只想登录用户,然后转到主页。
这是上面被调用的userService.login函数:
user-service.js
exports.login = function (user) {
return new Promise((resolve, reject) => {
httpModule.getJSON("http://5c55225d84df580014cd06a3.mockapi.io/users?search=" + user.email)
.then( resolve(response))
.catch((error) => { handleErrors(error); reject(); })
})
};
(为了进行测试,我仅在此示例中检查电子邮件是否存在“ Ally45”,因为JSON返回)
返回结果
我应该被重定向到主屏幕,但我从没到那里。
有人看到我要去哪里了吗?
谢谢。如此卡住...
约翰
答案 0 :(得分:0)
您的Http提取没有任何问题,这只是您处理响应的方式。当您点击该URL时,您的result
[
{
"id": "43",
"createdAt": "2019-02-01T05:54:43.766Z",
"username": "Ally45"
}
]
这是一个数组,但是您正在将其测试为result.id === undefined
,显然它是undefined
,并且您正在调用reject()
您的条件假设为result.length === 0 || result[0].id === void 0