简体   繁体   中英

ERROR TypeError: Cannot read property 'property' of undefined

I want to add objects to an array of objects.

So I created a variable and initialized it

articles: {}[] = [];

Then, I'm trying to add objects coming from a service

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

The problem occurs when I try to push an object to my Array: this.articles.push(article);

When I do this, I receive an error:

core.js:15723 ERROR TypeError: Cannot read property 'articles' of undefined

The JSON returned by the web service, look like that

[
    {
        "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="
    },
    {...}
]

The callback function that you're passing to articles.forEach is a normal function.

You should pass an Arrow Function( () => {} ) instead to retain the context of this .

Change 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);
    });
}

to this:

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

when you use function this is not known in function so this.articles is undefined, try to use arrow function:

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

You can simply assign response to array in a single line rather than a loop:

articles: any[] = [];

and then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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