代码之家  ›  专栏  ›  技术社区  ›  Jonathan Akwetey Okine

从JSON响应获取未定义

  •  0
  • Jonathan Akwetey Okine  · 技术社区  · 7 年前

    设置 客户 登记 .

      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
     }
    }
    }
    

    非常感谢您的帮助。

    谢谢您。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Sujeith Gopi Nath    7 年前

    在我的本地系统中尝试了你的代码,但我没有发现任何可疑的东西。 我想你最好在检索时检查一下代码的拼写 储蓄帐户ID . 标识-->标识 ?

    localStorage.setItem('savingsAccountID', this.clients['savingsAccountId'])
    
    localStorage.getItem('savingsAccountID');
    

    而且,从后端生成错误消息总是明智的,这将使您在将来避免陷入一片混乱。

        2
  •  0
  •   Ahmed Ibrahim    7 年前

    我看到key没有属性 clientId 在你的 json 是尸体 id json文件 客户ID 键,但不包含的键 savingsAccountId

    推荐文章