Shudy
Shudy

Reputation: 7936

Can't loop over a map from snapshot retrieved in Firebase Cloud Function

I'm lost with cloud funtions.

I'm trying to read from a table when new entry is written. Then parse some data from this table, and create a new object in a new table.

But I'm lost in some concepts with CF, and node...

At this moment I have the listeners to the table:

exports.createEventInvitation = functions.database
    .ref('event/{eventStartDate}/{eventId}/')
    .onWrite((change, context) => {
        const event = change.after.val();
        const eventUsersList = event.userList;
        ...
    })

The object event looks like this:

Event :{ conversationId: '8f6eb2b9-0cbb-4135-b6b6-c9f02c9aa91e',
  sharifyEventData: 
   { acceptationManual: false,
     address: 'ADDRESS FROM USER',
     adminProfileImageUrl: 'url_image',
     description: 'Description',
     eventEmoji: '💬',
     finalDate: '2019-11-12',
     finalHour: '09:30',
     headerImageUrl: '',
     initialDate: '2019-11-12',
     initialHour: '09:00',
     eventType: 'BEER',
     title: ''},
  eventID: '5f49ff65-bd98-45cb-a554-55da5c3c2f16',
  userList: { LeiUlbDlNKWwF6QPgnQiFTE03gt2: true },
  userType: 'NORMAL' }

I have to loop over the userlist to check which ID is the owner of the event. But I'm not able to loop it properly.

I tried to get the value like:

const userList = new Map()
const eventUsersList = event.userList
for( var entry in eventUsersList){
      if(entry[1]){
          userList.set(entry[0],3)
      }
}
console.log('UserListInvitation:' + util.inspect(userList, {showHidden: false, depth: null}))

And I achieved something like:

UserListInvitation:Map { 'L' => 3 }

But I'm missing something, because it seems I'm only taken the first letter of the key.

Also I tried:

for( var entryKey in eventUsersList.keys){
     if(eventUsersList[entryKey]){
       userList.set(entryKey,3)
     }
}

And this It's returning nothing

What I'm missing?

Upvotes: 0

Views: 109

Answers (1)

Shihab
Shihab

Reputation: 2679

If I understood you properly then the following code should work:

const userList = new Map()
const eventUsersList = event.userList
for( var key in eventUsersList){
  if(eventUsersList[key]){ // Safety check
    userList.set(key, 3) // key-value?
  }
}

Upvotes: 1

Related Questions