I am working on constructing an array based on data received from a subscription.
Currently, my code looks like this:
const dataFormat = new IData();
this.api.getData()
.pipe(
map(response => response),
tap(data => console.log('data array', data)))
.subscribe(dataResult => {
dataResult.forEach(function (item) {
dataFormat.title = item['title'];
dataFormat.author = item['author'];
dataFormat.date = item['date'];
this.listData.push(dataFormat);
});
});
When I try to push data into this.listData
, I encounter the following error:
Potentially invalid reference access to a class field via 'this.' of a nested function
To solve this issue, I tried using let self = this
with self.listData.push(dataFormat);
. However, this approach only stores the last item from the loop in my array.