E.Bolandian
E.Bolandian

Reputation: 573

Cloud functions - update data on realtime database

Since the breaking changes in nodejs (moved to nodejs version 8), i having a serious errors and problems with my code. I have looked at google documents how to rewrite the functions but i still can't managed it.

On nodejs version 6 i wrote a function that triggers when a new item is added and then update other nodes in the realtime database

For example

   // Keeps track of the length of the 'likes' child list in a separate property.
  exports.countlikechange = 
  functions.database.ref('/likes/{postid}/{userUID}').onWrite(event => {
   const collectionRef = event.data.ref.parent;
  const model = event.data.val();
  let genre = model.genre;
let videoID = model.videoID;
let userVideoID = model.userVideoID;

console.log("model: ",model);
console.log("genre: ",genre);
console.log("videoId: ",videoID);
console.log("userVideoID: ",userVideoID);

const countRef = collectionRef.child('likes');


// Return the promise from countRef.transaction() so our function 
// waits for this async event to complete before it exits.
 return countRef.transaction(current => {
  if (event.data.exists() && !event.data.previous.exists()) {
    const genreList = admin.database().ref(`${genre}/${videoID}/likes`).transaction(current => {
      return (current || 0) + 1;
    });
    const userList = admin.database().ref(`users/${userVideoID}/likes`).transaction(current => {
      return (current || 0) + 1;
    });
    const videoList = admin.database().ref(`videos/${userVideoID}/${videoID}/likes`).transaction(current => {
      return (current || 0) + 1;
    });
  }
 }).then(() => {
   console.log('Counter updated.');
   return null;
      });
   });

This func is no longer working because i have update nodejs to version 8.

On google documents the arguments changed, for example:

    exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite((change, context) => {

Also the return statment has change, it gives me error that i need to use promise. So im kinda confused, how should i rewrite this func so when it triggers i'll update nodes in the realtime databse.

Upvotes: 0

Views: 95

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317928

This doesn't actually have anything to do with the version of node. It has to do with the version of the firebase-functions SDK. You were using a very old pre-release version before. Since 1.0.0, the signatures have changed is a migration guide in documentation that describes the changes. In particular, read this section.

As of v 1.0 of the Firebase SDK for Cloud Functions, the event parameter for asynchronous functions is obsolete. It has been replaced by two new parameters: data and context.

You will need to learn the new APIs and port your code.

The requirements for return value have not changed. You are still obliged to return a promise the resolves when all the asynchronous work is complete in your function. If you are seeing a new error message about that, it's because you also upgraded your tools, and they are now checking unhandled promises for you.

Upvotes: 1

Related Questions