【问题标题】:ERROR TypeError: Cannot read property 'property' of undefined错误类型错误:无法读取未定义的属性“属性”
【发布时间】:2019-03-22 09:16:15
【问题描述】:

我想将对象添加到对象数组中。

所以我创建了一个变量并初始化了它

articles: {}[] = [];

然后,我正在尝试添加来自服务的对象

loadArticles(editionId) {
  this.articlesService.articlesGetArticlesForEdition(editionId).subscribe(articles => {
    console.log(articles);
    articles.forEach(function (article, i) {
      console.log(article);
      this.articles.push(article);
    });
    console.log(this.articles);
  });
}

当我尝试将对象推送到我的数组时出现问题:this.articles.push(article);

当我这样做时,我收到一个错误:

core.js:15723 错误类型错误:无法读取属性“文章” 未定义

web 服务返回的 JSON,如下所示

[
    {
        "category": "Editorial",
        "categoryFR": "Editorial",
        "shortCategory": "Editorial",
        "shortCategoryFR": "Editorial",
        "title": "Let’s go digital!",
        "titleFR": "Let’s go digital!",
        "chapo": "",
        "chapoFR": "",
        "bodyStyle": false,
        "body": "",
        "bodyFR": null,
        "body2": null,
        "body2FR": null,
        "mainVideo": null,
        "mainVideoCaption": null,
        "mainVideoCredits": null,
        "readingTime": "2",
        "edition": {
            "title": "Issue 1 - December 2018",
            "slogan": "The REYL Group Collaborators' Magazine",
            "mainPicture": null,
            "position": 1,
            "status": true,
            "id": "276f5c0f-346d-4ef5-a2cb-2711f6237bbe",
            "timestamp": "AAAAAAAACAU="
        },
        "date": "December 2018",
        "dateFR": "Décembre 2018",
        "theme": null,
        "articleAuthors": null,
        "author": null,
        "position": 0,
        "pictures": null,
        "mainPicture": {
            "id": "0716a4f4-4fcb-4cd7-b00c-d3cd0fbbc313",
            "name": "14ac31ef-7818-4739-b54c-eea4974e79dcIssue01_December2018_Edito_Image_01.jpg",
            "originalName": "Issue01_December2018_Edito_Image_01.jpg",
            "status": false
        },
        "smallPicture": {
            "id": "3c98ce44-4c9f-4383-b6a9-8a4759264c29",
            "name": "e0c9d687-e3b2-438f-8c25-d020ea4f0762Issue01_December2018_Image_Rubrique_Edito.jpg",
            "originalName": "Issue01_December2018_Image_Rubrique_Edito.jpg",
            "status": false
        },
        "gallery1": null,
        "gallery2": null,
        "galleries": null,
        "mainPictureCaption": null,
        "mainPictureCaptionFR": null,
        "mainPictureCredits": "© Adobe Stock/Vege",
        "mainPictureCreditsFR": null,
        "id": "1133f0bd-abe4-4278-9dc5-465956dc8e2d",
        "timestamp": "AAAAAAAACMQ="
    },
    {...}
]

【问题讨论】:

  • 改成articles: [] = [];
  • @JoelJoseph,我试过了,但没有任何改变

标签: arrays angular typescript


【解决方案1】:

您传递给articles.forEach 的回调函数是一个普通函数。

您应该传递一个箭头函数(() => {}) 来保留this 的上下文。

改变这个:

loadArticles(editionId) {
  this.articlesService.articlesGetArticlesForEdition(editionId)
    .subscribe(articles => {
      console.log(articles);
      articles.forEach(function(article, i) {
        console.log(article);
        this.articles.push(article);
      });
      console.log(this.articles);
    });
}

到这里:

loadArticles(editionId) {
  this.articlesService.articlesGetArticlesForEdition(editionId)
    .subscribe(articles => {
      console.log(articles);
      articles.forEach((article, i) => {
        console.log(article);
        this.articles.push(article);
      });
      console.log(this.articles);
    });
}

【讨论】:

  • 是的,它有效,感谢您的解释! (必须等待 5 分钟才能接受您的回答)
  • @118218 哦,我知道这是正确的答案,直到箭头函数每个新的 JavaScript 函数都定义了自己的 this
【解决方案2】:

当你使用函数this在函数中是未知的,所以this.articles是未定义的,尝试使用箭头函数:

articles.forEach((article, i)=> {
      console.log(article);
      this.articles.push(article);
    });

【讨论】:

【解决方案3】:

您可以简单地将响应分配给一行而不是循环:

articles: any[] = [];

然后

Service.articlesGetArticlesForEdition(editionId).subscribe(articles => 
{
   for (let art of articles) {
     this.articles.push(art);
   }
}

【讨论】:

  • 不,实际上我想应用一些过滤器,而不是分配整个响应。
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2021-12-03
  • 2021-11-22
  • 2018-07-12
  • 2021-12-16
  • 2020-08-16
相关资源
最近更新 更多