Lewis Black
Lewis Black

Reputation: 987

Cloud Functions for Firebase: How to access an array I have just got from firebase?

I am downloading from firebase in my function and I can't work out how to access the array I have download.

This is my code:

exports.messageAdded = functions.database.ref('cities/barcelona/pickUp/gamesDetailed/{gameId}/wal/{messageId}').onWrite(event => {


     const getPlayer1sIdPromise = admin.database().ref(`/cities/barcelona/pickUp/gamesDetailed/gameId/pla1`).once('value');
     const getPlayer2sIdPromise = admin.database().ref(`/cities/barcelona/pickUp/gamesDetailed/gameId/pla2`).once('value');


     const getInstanceIdPromise = admin.database().ref(`/users/usersDetailed/${receiverUid}/deviceID`).once('value');

     return Promise.all([getPlayer1sIdPromise, getPlayer2sIdPromise]).then(results => {
        const player1Ids = results[0].val();

       // PROBLEM IS HERE

        console.log(player1Ids);
        console.log(player1Ids[0]);
        console.log(results[0]);
        console.log(results[0].val()[0]);


        Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results => {
             const instanceId = results[0].val();
             const sender = results[1];

             const payload = {
             notification: {
             title: senderName,
             body: messageText,
             icon: sender.photoURL
             }
             };

             admin.messaging().sendToDevice(instanceId, payload)
             .then(function (response) {
                   console.log("Successfully sent message:", response.results);

                   console.log("Successfully sent message:", response);
                   })
             .catch(function (error) {
                    console.log("Error sending message:", error);
                    });
             });

       });

  });

In the code I state a point where the problem is. There I can't work out how to access player1Ids. The thing I get back should be an array and i'd like to access the first value but I don't know how to do that.

This is completely down to no experience in Javascript and I apologise if this a repeated question but I looked and couldn't seem to find one but I didn't know too much of what to search.

This is the dataStructure I am using:

  root/
  |___ ...
  |      |___ pla1
  |      |       |___ USER_ID_HERE: userName
  |      |       |___ ...
  |      |___ pla2
  |              |___ USER_ID_HERE: userName
  |              |___ ...

Definitely in my firebase database there is an array there so hopefully I can find out how to access it.

Thanks in advance.

Upvotes: 0

Views: 745

Answers (1)

user8606263
user8606263

Reputation:

From what I can see here, there are a few problems in your code. Did you check in the Firebase console the error messages ? It might give us some nice hints here.

First I cannot see getPlayer2sIdPromise defined in your code, but it seems like you are using it in your Promise.all() call.

That might be the reason why your code doesn't go further.

Anyway, you would do it like this (I removed a few parts of your code just to focus on the important part) :

    exports.messageAdded = functions.database.ref('cities/barcelona/pickUp/gamesDetailed/{gameId}/wal/{messageId}').onWrite(event => {

         const getPlayerOneID = admin.database().ref(`/cities/barcelona/pickUp/gamesDetailed/gameId/pla1`).once('value');

          return Promise.all([getPlayerOneID]).then(results => {

              const playerOneIdSnapshot = results[0];

              // ...And then you can access the value like so :

              const playerOneIdSnapshotValue = playerOneIdSnapshot.val();

          });

   });


Now from what I can see your database structure looks like this :

root/
|___ ...
|      |___ pla1
|              |___ userId: USER_ID_HERE
|              |___ ...

In that case, you would access the user ID like so :

const userID = playerOneIdSnapshotValue.userId;

It might be a little be different depending on how you set up your database !

Let me know if it works :)

Upvotes: 2

Related Questions