简体   繁体   English

类型“ {}”上不存在属性“ listName”

[英]Property 'listName' does not exist on type '{}'

I get theProperty 'listName' does not exist on type '{}' when I run the following code inside my details page. 当我在详细信息页面中运行以下代码时,得到的属性“ listName”在类型“ {}”上不存在。 It seems something trivial, but I spent the entire afternoon not able to understand what is going wrong here. 看起来有些琐碎,但整个下午我都无法理解这里出了什么问题。

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import {ListDetailsService} from '../../providers/list-details-service';


@Component({
  selector: 'page-item-details',
  templateUrl: 'item-details.html'
})
export class ItemDetailsPage {
  selectedItem: any;
  detailsItems : Array<{title: string, note: string, id: string}>;

  constructor(public navCtrl: NavController, public navParams: NavParams, public listDetailsService : ListDetailsService) {
    // If we navigated to this page, we will have an item available as a nav param
    this.selectedItem = navParams.get('item');

    this.detailsItems = [];
    this.listDetailsService.load(this.selectedItem.id).then(myListItems => {
      console.log(myListItems);
      console.log(myListItems.listName);


    }).catch(function(e){
      console.log(e);
    })
  }
}

When I run console.log(myListItems), I get the following data. 当我运行console.log(myListItems)时,我得到以下数据。

{
items: 
   [ { _id: 58bbf7fd463667f51d7804f6,
       votes: '10',
       item: 'Apple Iphone' },
     { _id: 58bbf7fd463667f51d7804f5,
       votes: '2',
       item: 'Google Phone' },
     { _id: 58bbf7fd463667f51d7804f4,
       votes: '2',
       item: 'Samsung Phone' },
     { _id: 58bbf7fd463667f51d7804f3, votes: '6', item: 'LG Phone' } ],
  __v: 0,
  listName: 'Best Phones',
  _id: 58bbf7fd463667f51d7804f2 }

However, when I run 但是,当我跑步时

console.log(myListItems.listName);

I get 我懂了

Property 'listName' does not exist on type '{}'.

Even though the property is present in the object myListNames 即使该属性存在于对象myListNames中

I am sure I am missing something trivial here. 我确定我在这里缺少一些琐碎的东西。 But I spend the entire afternoon, not knowing the answer. 但是我花了整个下午,不知道答案。

Your help is much appreciated! 非常感谢您的帮助!

This is a TypeScript compiler error, and you should probably change the return type of your listDetailsService.load function to either any or an interface you define which matches the object of your list. 这是TypeScript编译器错误,您应该将listDetailsService.load函数的返回类型更改为与列表对象匹配的any或定义的接口。

export class ListDetailsService {

  //...
  load(): any {
      //...
  }  

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM