Encountering the following error log
while executing a firebase function to fetch documents and values from the recentPosts
array field.
Error: Unknown error status: Error: Unknown error status: TypeError: elements.get is not a function
at new HttpsError (/srv/node_modules/firebase-functions/lib/providers/https.js:90:19)
at admin.firestore.collectionGroup.where.get.then.catch.error (/srv/lib/index.js:71:15)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
Class:
class PostTable {
public commentCount : number;
public dateTime : number;
public docId : string;
public post : string;
public userId : string;
public userName : string;
constructor(commentCount: number, dateTime: admin.firestore.Timestamp, docId: string, post : string, userId : string, userName : string) {
this.commentCount = commentCount
this.dateTime= dateTime.toDate().getTime()
this.docId=docId
this.post=post
this.userId=userId
this.userName=userName
}
}
function:
export const getPosts = functions.https.onCall((data, context) => {
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
let responseCollection : PostTable[] = []
admin.firestore().collectionGroup('recentPostColl').where('users', "array-contains",context.auth.token.name)
.get()
.then(collectionOfPosts => {
if(!collectionOfPosts.empty)
{
collectionOfPosts.forEach(element => {
let collection : Map<String, Object>[] = element.get('recentPosts')
collection.forEach(elements => {
try {
const p : PostTable = new PostTable(elements.get('commentCount')as number, elements.get('dateTime') as admin.firestore.Timestamp
,elements.get('docId') as string,elements.get('post') as string, elements.get('userId') as string, elements.get('userName') as string);
const stamp : admin.firestore.Timestamp = elements.get('dateTime') as admin.firestore.Timestamp
const date : Date = stamp.toDate()
if(date.getTime() > new Date(data.date).getTime())
{
responseCollection.push(p)
}
} catch (error) {
throw new functions.https.HttpsError(error, 'Constructor error');
}
});
});
}
})
.catch(error =>{
throw new functions.https.HttpsError(error, 'code error');
})
return responseCollection
})
Document: