How to access items from a Map within an Array in Cloud Functions?

Asked

Viewed 11 times

0

In firebase project I have an array messagescontaining a Map with the fields message, timestamp and type. I’m trying to access these fields with the lastMessage: data.messages; however the console.log returns me as Undefined. Does anyone know any way to access these fields?

exports.onConversationCreated = functions.firestore.document('chat/{chatId}')
.onCreate((snapshot, context) => {
     let data = snapshot.data();
     let chatId = context.params.chatId;

    if(data){
        let members = data.members;
        let lastMessage = data.messages;
    for(let index = 0; index < members.length; index++){
        let currentUserId = members[index];
        let remainingUsersId = members.filter((u) => u != currentUserId);
        console.log(lastMessage.message);
        remainingUsersId.forEach((m) => {
            return admin.firestore().collection('authUsers').doc(m).get().then( (_doc) => {
                let userData = _doc.data();
                if(userData) {

                    return admin.firestore().collection("authUsers")
                    .doc(currentUserId).collection('chat').doc(m).create({
                        "chatId": chatId,
                        "image": userData.photoUrl,
                        "name": userData.displayName,
                        "unseenCount": 0
                    });
                }
                return null;

            }).catch(() => {return null});
        })
    }
}
return null;

});

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.