angular - 从 JSON 响应中获取未定义
问题描述
设置为客户注册。这是注册客户端的函数。
registerAsClient(){
this.loading =this.loadingCtrl.create({
content:"Setting up Account"
});
this.loading.present();
this.buildClientData();
console.log(this.clientData);
this.auth.store('clients', this.clientData).subscribe((response)=>{
this.clients = response.json();
这是为了在注册到控制台后显示服务器的响应。
console.log("Client ID : " + this.clients['clientId']);
console.log("Savings Account ID : " + this.clients['savingsAccountId']);
localStorage.setItem('clientID', this.clients['clientId']);
localStorage.setItem('officeID', this.clients['officeId']);
localStorage.setItem('savingsAccountID', this.clients['savingsAccountId']);
setTimeout(()=>{
this.loading.dismissAll();
this.toastrCtrl.messenger('Successfully Registered');
localStorage.setItem('registered', 'true');
this.navCtrl.push(HomePage);
},5000)
},error=>{
if(error.status === 403){
this.loading.dismissAll();
this.toastrCtrl.messenger("Phone number already exists. Please use another");
}else
this.loading.dismissAll();
this.toastrCtrl.messenger('Service unavailable. Please try again later');
console.log(error);
// alert(JSON.stringify("Error is :" + error));
})
}
这是来自控制台注册的响应,我能够获取客户 ID,但储蓄账户 ID 显示为未定义。
Client ID : 104
Savings Account ID : undefined
使用端点进行测试时的总json
响应。
{
"id": 89,
"accountNo": "000000089",
"status": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
},
"subStatus": {
"active": false,
"mandatory": false
},
"active": true,
"activationDate": [
2018,
8,
13
],
"firstname": "Albertina",
"lastname": "Ben-Patterson",
"displayName": "Albertina Ben-Patterson",
"mobileNo": "0201234598",
"gender": {
"active": false,
"mandatory": false
},
"clientType": {
"active": false,
"mandatory": false
},
"clientClassification": {
"active": false,
"mandatory": false
},
"isStaff": false,
"officeId": 10,
"officeName": "Greater Accra Region",
"timeline": {
"submittedOnDate": [
2018,
8,
13
],
"submittedByUsername": "admin",
"submittedByFirstname": "App",
"submittedByLastname": "Administrator",
"activatedOnDate": [
2018,
8,
13
],
"activatedByUsername": "admin",
"activatedByFirstname": "App",
"activatedByLastname": "Administrator"
},
"savingsAccountId": 8,
"groups": [],
"clientNonPersonDetails": {
"constitution": {
"active": false,
"mandatory": false
},
"mainBusinessLine": {
"active": false,
"mandatory": false
}
}
}
任何帮助深表感谢。
谢谢你。
解决方案
在我的本地系统中尝试了您的代码,但我没有发现任何可疑之处。我想您最好在检索SavingAccountId时检查代码拼写。身份证——>身份证?
localStorage.setItem('savingsAccountID', this.clients['savingsAccountId'])
localStorage.getItem('savingsAccountID');
此外,始终建议从后端生成错误消息,这将使您在未来免于一团糟。
推荐阅读
- java - JdbcTestUtils 和事务上下文测试
- debugging - 调试触发的 modsecurity 规则命中
- python - Python相关问题 - 用户输入日期/N00B/
- .htaccess - 如果uri是root,需要在htaccess中设置env
- php - PHP 注意:遇到格式不正确的数值 - 过时的代码?
- python - Pandas 分析 - KeyError:“请求的级别(var1)与索引名称(无)不匹配”
- laravel - 语法错误或访问冲突:1055 'adminlte3.orders.name' 不在 GROUP BY 中
- typescript - 如何定义仅接受某些用户定义类的实例的 Typescript 类型
- google-bigquery - 如何安排 GBQ 查询并每天将数据导出到 Excel 文件?
- nginx - 修复 nginx 配置以允许 iframe