无法读取未定义的属性消息

时间:2017-12-04 12:59:37

标签: angular angular-cli

在尝试使用angular的新httpclient从api中获取数据时,我得到错误无法读取未定义的属性消息

// Here is my interface
export interface IMessage {
    Messages: Messages[];
}

interface Messages {
    id: number;
    messageContent: string;
    fromUserId: string;
    toUserId: string;
    productId?: number;
    parentMessageId?: number;
    subject: string;
    styleNumber?: string;
}

//调用此api的方法

getAllMessages(): Observable<IMessage[]> {
    this.messageRepositoryService.getAllMessages()
    .subscribe(data => {
      console.log('data is =>', data[1].Messages); // here is error
      this.messages = data;
    },
    err => {
      console.log('Error occured while fetching country List');
    });
    return;
  }

  // in the service
getAllMessages(): Observable<IMessage[]> {
        return this.http.get<IMessage[]>(`${this.ROOT_URL}/list`);
    }

要查看响应从api看起来如何,这是网址http://52.70.78.117:7111/api/message/list

Here is how my object looks

{"data":{"Messages":[{"Id":1,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"Test Message","StyleNumber":null},{"Id":2,"MessageContent":"test data","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"Test Message by Dev Team","StyleNumber":null},{"Id":3,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":4,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":5,"MessageContent":"testtesttesttest","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":6,"MessageContent":"testtesttesttest","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":7,"MessageContent":"testtesttesttest","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":8,"MessageContent":"Test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":9,"MessageContent":"Test Message","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":10,"MessageContent":"Test Message","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":11,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e58696b1c943-ade0-4026-b8ed-0f4a","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"Test Message","StyleNumber":null},{"Id":12,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e586","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"96b1c943-ade0-4026-b8ed-0f4a6231e586","StyleNumber":null},{"Id":13,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e586","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"96b1c943-ade0-4026-b8ed-0f4a6231e586","StyleNumber":null},{"Id":14,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e586","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"96b1c943-ade0-4026-b8ed-0f4a6231e586","StyleNumber":null},{"Id":15,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e586","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"96b1c943-ade0-4026-b8ed-0f4a6231e586","StyleNumber":null},{"Id":16,"MessageContent":"96b1c943-ade0-4026-b8ed-0f4a6231e586","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"96b1c943-ade0-4026-b8ed-0f4a6231e586","StyleNumber":null},{"Id":17,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"test","StyleNumber":null},{"Id":18,"MessageContent":"test message","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"test message","StyleNumber":null},{"Id":19,"MessageContent":"test data","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"Test Message by Dev Team","StyleNumber":null},{"Id":20,"MessageContent":"test message","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"test message","StyleNumber":null},{"Id":21,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"test mness","StyleNumber":null},{"Id":22,"MessageContent":"test","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"test ","StyleNumber":null},{"Id":25,"MessageContent":"test message","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":1,"Subject":"test message","StyleNumber":null},{"Id":28,"MessageContent":"test data tsn","FromUserId":"74df8f98-2925-4242-8d32-8b09f3691396","UserName":"365@test.com","ToUserId":"96b1c943-ade0-4026-b8ed-0f4a6231e586","ProductId":null,"ParentMessageId":null,"Subject":"Test Message by Dev Team","StyleNumber":null}],"Pagination":{"Page":1,"PageLength":20,"TotalRecords":20,"TotalPages":1},"MessageLimit":{"TotalMessageCount":5,"RemainingMessageCount":3}}}

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试:

data.data.Messages[1]

数据是一个对象,而不是数组。消息是数组。

答案 1 :(得分:0)

您需要访问data.Messages等数据

getAllMessages(): Observable<IMessage[]> {
    this.messageRepositoryService.getAllMessages()
    .subscribe((data:any) => {
      console.log('data is =>', data.data.Messages); 
      this.messages = data.data.Messages;
    },
    err => {
      console.log('Error occured while fetching country List');
    });
    return;
  }