There is featureImages section in component.html which is throwing this error not sure why it is happening
news-section.component.ts:296 ERROR TypeError: Cannot read properties of undefined (reading ‘value’)
at NewsSectionComponent_div_1_div_4_div_1_Template (news-section.component.html:35:14)
at ReactiveLViewConsumer.runInContext (core.mjs:10425:13)
at executeTemplate (core.mjs:10968:22)
at refreshView (core.mjs:12489:13)
at detectChangesInView (core.mjs:12653:9)
at detectChangesInEmbeddedViews (core.mjs:12597:13)
at refreshView (core.mjs:12513:9)
at detectChangesInView (core.mjs:12653:9)
at detectChangesInEmbeddedViews (core.mjs:12597:13)
at refreshView (core.mjs:12513:9)
Getting this error for a property when a user click on like button, but not on initial page load.
Also like button has a different function which is not related to initial page loading function
featuredImages = [];
featureNids = [];
constructor(
private restService: RestService,
private gtmService: GtmService,
private jsonApiService: JsonApiService,
public hubService: HubDataService,
private http: HttpClient,
public router: Router
) {}
ngOnInit() {
this.getStoriesToDisplay();
}
public getStoriesToDisplay() {
// Try to pull from saved articles
const index = this.newsFilterOptions.indexOf(this.currentOption);
if (this.savedResults[index] && this.savedResults[index].length > 0) {
this.storiesToDisplay = this.savedResults[index];
} else {
let restType;
if (this.currentOption === 'Current') {
restType = RestServiceType.GlobalNews;
} else {
restType = RestServiceType.GlobalNewsCategory + this.currentOption;
}
const responsePromise = this.restService.getPagedJsonApiContent(
window.location.protocol + '//' + window.location.host,
restType,
this.amountToShow,
0
);
responsePromise.then((resp) => {
if (!resp.ok) {
return Promise.reject(resp.statusText);
} else {
this.http
.get('/get-featured-news-data?_format=json')
.subscribe((res) => {
if (Object.values(res)?.length > 0) {
let sortedItemIds = Object.values(res);
let splitedArray =
sortedItemIds[0]['field_top_featured_news'].split('+');
splitedArray = [...new Set(splitedArray)];
let topFeaturedNews: string = res[0]['field_top_featured_news'];
this.http
.get(
'/get-featured-news-details-rev/' +
topFeaturedNews +
'?_format=json'
)
.subscribe((res) => {
if (Object.values(res['rows'])?.length > 0) {
const obj = {};
this.featuredImages = res['imagerows'];
console.log('this.featureImages', this.featuredImages);
Object.values(res['rows']).forEach((o: any) => {
obj[o.nid[0]['value']] = o;
});
// sorted according to added from back-end
let sortedFeaturedNewsList = splitedArray.map((e) =>
Object.assign({}, obj[e])
);
// remove the empty object if it is unpublished news
sortedFeaturedNewsList = sortedFeaturedNewsList.filter(
(value) => Object.keys(value).length !== 0
);
for (let i = 0; i < Object.values(res)[0]?.length; i++) {
this.featureNids.push(
Object.values(res)[0][i].nid[0]['value']
);
}
this.fnLoaded = true;
this.featurednewstoDisplay.push(sortedFeaturedNewsList);
this.featuredStoriesToDisplay =
this.featurednewstoDisplay[0];
console.log('this.featuredStoriesToDisplay', this.featuredStoriesToDisplay);
if (this.featuredStoriesToDisplay?.length > 0) {
let ids = this.featuredStoriesToDisplay
.map((a) => a.nid[0]['value'])
.map((i) => Number(i));
this.story(resp, index, ids);
} else {
this.story(resp, index);
}
} else {
this.story(resp, index);
}
});
} else {
this.story(resp, index);
}
});
}
});
}
}
Function called on like button
toggleLike(userId: string, featurednews): void {
if (featurednews && featurednews.nid) {
if (this.isLikedBy(userId, featurednews.field_users_that_liked)) {
// remove current user like
let i = 0;
while (i < featurednews.field_users_that_liked.length) {
if (featurednews.field_users_that_liked[i].value === userId) {
featurednews.field_users_that_liked.splice(i, 1);
}
i++;
}
} else {
featurednews.field_users_that_liked.push({ value: userId });
}
// featurednews.nid = featurednews.nid[0].value;
if (featurednews.nid[0] === undefined || featurednews.nid[0] === null) {
featurednews.nid = featurednews.nid;
} else {
featurednews.nid = featurednews.nid[0].value;
}
let usersThatLiked = [];
featurednews['field_users_that_liked'].forEach((element) => {
usersThatLiked.push(element);
});
featurednews.usersThatLiked = usersThatLiked;
this.restService.getRestSessionToken().subscribe((token) => {
const requestBody = JSON.stringify(featurednews);
this.restService
.patchResource(
`${RestServiceType.NodeData}${featurednews.nid}?_format=json`,
requestBody,
token
)
.subscribe();
});
}
}