Abhishek
Abhishek

Reputation: 1261

Firebase Function - Unable to read all child values


I have a firebase function as per below

    exports.shareNotifyNCoin = functions.database.ref('{userID}/mySharedImages/{postID}')
.onCreate(event => {
  const userID = event.params.userID
  const postID = event.params.postID
  const createdAt = postID.slice(0,15)
  const sharedBenefits = event.data.child('sharedBenefits').val()
  const sharedOnFaceBook = event.data.child('facebook').val() 
  const sharedOnTwitter = event.data.child('twitter').val()
  const sharedOnPinterest = event.data.child('pinterest').val()
  const createdByPic = event.data.child('myPhotoUrl').val()
console.log('myPhotoUrl is :' + createdByPic)
  console.log('sharedBenefits is : ' + sharedBenefits)
  console.log('facebook is:' + sharedOnFaceBook)
  console.log('twitter is: ' + sharedOnTwitter)
  console.log('pinterest is: ' + sharedOnPinterest)
    }

it is working on a firebase realtime database which has nodes as per below:

data structure

This function triggers every time a new value is inserted as per the structure in the image. The problem I am having is that when a new node is created then the value of 'sharedBenefits' is being retrieved successfully but not the other child values of the snapshot. Output of the logs is as per the image here.

functions logs

I am unable figure out the reason why I am not able to retrieve these Child values of the snapshot.

Just FYI, the code that is writing to database is in Java for Android as per below:

public static void saveToDatabase(Boolean fb, Boolean tw, Boolean pin, String shareUrl, String shareType) {
    setShareImageFirebaseReference();
    String nodeID = dateTimeStampMilliSeconds();
    myRef.child(nodeID).child(shareType).setValue(shareUrl);
    myRef.child(nodeID).child(FACEBOOK).setValue(fb);
    myRef.child(nodeID).child(TWITTER).setValue(tw);
    myRef.child(nodeID).child(PINTEREST).setValue(pin);
    myRef.child(nodeID).child(MY_PHOTO).setValue(thisAppUser.getPhotoUrl());
}

Upvotes: 0

Views: 233

Answers (1)

nikhilkachare01
nikhilkachare01

Reputation: 58

I believe cloud function you wrote is being triggered when you setValue of shareUrl on {userID}/mySharedImages/{postID} node as trigger is onCreate of that node and at that time no other values are set, thats why the error.

You should try fanout technique to upload all values at same time or update whole object together not single values.

More about fanout technique here

Upvotes: 1

Related Questions