In my ionic app, I am trying to retrieve an existing record, change some values, & then update the record with those values.
Below is the code which I’m copying from a tutorial:
admin.firestore().collection('posts').doc(postId).get().then((data) => {
let likesCount = data.data()?.likesCount || 0;
let likes = data.data()?.likes || [];
let updateData = {};
if (action == 'like') {
updateData['likesCount'] = ++likesCount;
updateData[`likes.${userId}`] = true;
} else {
updateData['likesCount'] = --likesCount;
updateData[`likes.${userId}`] = false;
}
admin.firestore().collection('posts').doc(postId).update(updateData).then(() => {
response.status(200).send('Done');
}).catch((err) => {
response.status(err.code).send(err.message);
});
}).catch((err) => {
response.status(err.code).send(err.message);
});
The issue I’m facing is that the below errors are appearing within the If & Else blocks when I try to assign the new values:
Element implicitly has an ‘any’ type because expression of type ‘“likesCount”’ can’t be used to index type ‘{}’.
Property ‘likesCount’ does not exist on type ‘{}’.
Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘{}’.
No index signature with a parameter of type ‘string’ was found on type ‘{}’.
Can someone please tell me how to resolve this issue?
FYI, there are more than these 2 fields within the data that I’m retrieving from firestore. So I want to keep those values in the firebase entry, & only update the 2 fields mentioned above.