财产' todos'类型'对象'上不存在

时间:2018-06-02 04:06:48

标签: angular rest mean-stack

我正在构建我的第一个MEAN堆栈来做应用程序。我希望我能正确地表达我的问题,因为我是新手。

错误来自我的getList()。subscribe函数。我想我明白错误试图告诉我什么,但我不知道如何解决它。我试过分配

todos: any;

但那并没有奏效。然后我尝试过分配

todos: Array<any>;

和许多其他不同的语法;没运气。

dashboard.component.ts

export class DashboardComponent implements OnInit {
  user: Object;

  lists: any;
  todos: any;

  @Input() input;

  @Output() update = new EventEmitter();

  constructor(
    private authService: AuthService,
    private flashMessage: FlashMessagesService,
    private listService: ListService
  ) { }

  ngOnInit() {
    this.getLists();

    this.authService.getProfile().subscribe(profile => {
      this.user = profile.user;
    },
    err => {
      console.log(err);
      return false;
    });
  }

  getLists(): void {
    this.listService.getAllTodos()
      .subscribe(data => this.lists = data.todos);
  }

list.service.ts

export class ListService {
  newTodo: any;
  list: any;

  constructor(private http: HttpClient) { }

  getAllTodos() {
    return this.http.get('http://localhost:3000/todos/lists');
  }

编辑 ======================================= ===========

这是我的自定义界面/类模型,我尝试使用更改的编码来订阅可观察的

List.ts

export class List {
  item: string;
  completed: boolean;
}

更改为我的旧文件

dashboard.component.ts

import { List } from '../../list';

export class DashboardComponent implements OnInit {

  lists: List;

getLists(): void {
    this.listService.getAllTodos()
      .subscribe((data: List) => this.lists = data.todos);
  }

我仍然在主题行中发布了相同的错误,但类型更改为&#39;对象&#39;到&#39;列出&#39;

Property 'todos' does not exist on type 'List'

1 个答案:

答案 0 :(得分:2)

您可以使用自定义 interface/class 来映射回复。或者在访问待办事项之前使用任何类型。

 this.listService.getAllTodos()
      .subscribe((data:any) => this.lists = data.todos);