Firebase cloud function not updating record










0















imagine this scenario:



You have a list with lets say 100 items, a user favorites 12 items from the list.



The user now wants these 12 items displayed in a new list with the up to date data (if data changes from the original list/node)



What would be the best way to accomplish this without having to denormalize all of the data for each item?



is there a way to orderByChild().equalTo(multiple items)



(the multiple items being the favorited items)



Currently, I am successfully displaying the favorites in a new list but I am pushing all the data to the users node with the favorites, problem is when i change data, their data from favorites wont change.



note - these changes are made manually in the db and cannot be changed by the user (meta data that can potentially change)




UPDATE



I'm trying to achieve this now with cloud functions. I am trying to update the item but I can't seem to get it working.



Here is my code:



exports.itemUpdate = functions.database
.ref('/items/itemId')
.onUpdate((change, context) =>

const before = change.before.val(); // DataSnapshot after the change
const after = change.after.val(); // DataSnapshot after the change

console.log(after);

if (before.effects === after.effects)
console.log('effects didnt change')
return null;

const ref = admin.database().ref('users')
.orderByChild('likedItems')
.equalTo(before.title);

return ref.update(after);
);


I'm not to sure what I am doing wrong here.



Cheers!










share|improve this question




























    0















    imagine this scenario:



    You have a list with lets say 100 items, a user favorites 12 items from the list.



    The user now wants these 12 items displayed in a new list with the up to date data (if data changes from the original list/node)



    What would be the best way to accomplish this without having to denormalize all of the data for each item?



    is there a way to orderByChild().equalTo(multiple items)



    (the multiple items being the favorited items)



    Currently, I am successfully displaying the favorites in a new list but I am pushing all the data to the users node with the favorites, problem is when i change data, their data from favorites wont change.



    note - these changes are made manually in the db and cannot be changed by the user (meta data that can potentially change)




    UPDATE



    I'm trying to achieve this now with cloud functions. I am trying to update the item but I can't seem to get it working.



    Here is my code:



    exports.itemUpdate = functions.database
    .ref('/items/itemId')
    .onUpdate((change, context) =>

    const before = change.before.val(); // DataSnapshot after the change
    const after = change.after.val(); // DataSnapshot after the change

    console.log(after);

    if (before.effects === after.effects)
    console.log('effects didnt change')
    return null;

    const ref = admin.database().ref('users')
    .orderByChild('likedItems')
    .equalTo(before.title);

    return ref.update(after);
    );


    I'm not to sure what I am doing wrong here.



    Cheers!










    share|improve this question


























      0












      0








      0








      imagine this scenario:



      You have a list with lets say 100 items, a user favorites 12 items from the list.



      The user now wants these 12 items displayed in a new list with the up to date data (if data changes from the original list/node)



      What would be the best way to accomplish this without having to denormalize all of the data for each item?



      is there a way to orderByChild().equalTo(multiple items)



      (the multiple items being the favorited items)



      Currently, I am successfully displaying the favorites in a new list but I am pushing all the data to the users node with the favorites, problem is when i change data, their data from favorites wont change.



      note - these changes are made manually in the db and cannot be changed by the user (meta data that can potentially change)




      UPDATE



      I'm trying to achieve this now with cloud functions. I am trying to update the item but I can't seem to get it working.



      Here is my code:



      exports.itemUpdate = functions.database
      .ref('/items/itemId')
      .onUpdate((change, context) =>

      const before = change.before.val(); // DataSnapshot after the change
      const after = change.after.val(); // DataSnapshot after the change

      console.log(after);

      if (before.effects === after.effects)
      console.log('effects didnt change')
      return null;

      const ref = admin.database().ref('users')
      .orderByChild('likedItems')
      .equalTo(before.title);

      return ref.update(after);
      );


      I'm not to sure what I am doing wrong here.



      Cheers!










      share|improve this question
















      imagine this scenario:



      You have a list with lets say 100 items, a user favorites 12 items from the list.



      The user now wants these 12 items displayed in a new list with the up to date data (if data changes from the original list/node)



      What would be the best way to accomplish this without having to denormalize all of the data for each item?



      is there a way to orderByChild().equalTo(multiple items)



      (the multiple items being the favorited items)



      Currently, I am successfully displaying the favorites in a new list but I am pushing all the data to the users node with the favorites, problem is when i change data, their data from favorites wont change.



      note - these changes are made manually in the db and cannot be changed by the user (meta data that can potentially change)




      UPDATE



      I'm trying to achieve this now with cloud functions. I am trying to update the item but I can't seem to get it working.



      Here is my code:



      exports.itemUpdate = functions.database
      .ref('/items/itemId')
      .onUpdate((change, context) =>

      const before = change.before.val(); // DataSnapshot after the change
      const after = change.after.val(); // DataSnapshot after the change

      console.log(after);

      if (before.effects === after.effects)
      console.log('effects didnt change')
      return null;

      const ref = admin.database().ref('users')
      .orderByChild('likedItems')
      .equalTo(before.title);

      return ref.update(after);
      );


      I'm not to sure what I am doing wrong here.



      Cheers!







      reactjs list firebase favorites






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 21:22







      hugger

















      asked Nov 15 '18 at 16:20









      huggerhugger

      447




      447






















          1 Answer
          1






          active

          oldest

          votes


















          0














          There isn't a way to do multiple items in the orderByChild, denormalising in NoSQL is fine its just about keeping it in sync like you mention. I would recommend using a Cloud Function which will help you to keep things in sync.



          Another option is to use Firestore instead of the Realtime Database as it has better querying capabilities where you could store a users id in the document and using an array contains filter you could get all the users posts.



          The below is the start of a Cloud function for a realtime database trigger for an update of an item.



          exports.itemUpdate = functions.database.ref('/items/itemId')
          .onUpdate((snap, context) =>
          // Query your users node for matching items and update them
          );





          share|improve this answer























          • Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

            – hugger
            Nov 16 '18 at 2:51











          • There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

            – Jack Woodward
            Nov 16 '18 at 9:17











          • Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

            – hugger
            Nov 16 '18 at 16:38











          • If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

            – Jack Woodward
            Nov 16 '18 at 17:49











          • Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

            – hugger
            Nov 16 '18 at 21:25










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323729%2ffirebase-cloud-function-not-updating-record%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          There isn't a way to do multiple items in the orderByChild, denormalising in NoSQL is fine its just about keeping it in sync like you mention. I would recommend using a Cloud Function which will help you to keep things in sync.



          Another option is to use Firestore instead of the Realtime Database as it has better querying capabilities where you could store a users id in the document and using an array contains filter you could get all the users posts.



          The below is the start of a Cloud function for a realtime database trigger for an update of an item.



          exports.itemUpdate = functions.database.ref('/items/itemId')
          .onUpdate((snap, context) =>
          // Query your users node for matching items and update them
          );





          share|improve this answer























          • Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

            – hugger
            Nov 16 '18 at 2:51











          • There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

            – Jack Woodward
            Nov 16 '18 at 9:17











          • Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

            – hugger
            Nov 16 '18 at 16:38











          • If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

            – Jack Woodward
            Nov 16 '18 at 17:49











          • Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

            – hugger
            Nov 16 '18 at 21:25















          0














          There isn't a way to do multiple items in the orderByChild, denormalising in NoSQL is fine its just about keeping it in sync like you mention. I would recommend using a Cloud Function which will help you to keep things in sync.



          Another option is to use Firestore instead of the Realtime Database as it has better querying capabilities where you could store a users id in the document and using an array contains filter you could get all the users posts.



          The below is the start of a Cloud function for a realtime database trigger for an update of an item.



          exports.itemUpdate = functions.database.ref('/items/itemId')
          .onUpdate((snap, context) =>
          // Query your users node for matching items and update them
          );





          share|improve this answer























          • Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

            – hugger
            Nov 16 '18 at 2:51











          • There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

            – Jack Woodward
            Nov 16 '18 at 9:17











          • Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

            – hugger
            Nov 16 '18 at 16:38











          • If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

            – Jack Woodward
            Nov 16 '18 at 17:49











          • Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

            – hugger
            Nov 16 '18 at 21:25













          0












          0








          0







          There isn't a way to do multiple items in the orderByChild, denormalising in NoSQL is fine its just about keeping it in sync like you mention. I would recommend using a Cloud Function which will help you to keep things in sync.



          Another option is to use Firestore instead of the Realtime Database as it has better querying capabilities where you could store a users id in the document and using an array contains filter you could get all the users posts.



          The below is the start of a Cloud function for a realtime database trigger for an update of an item.



          exports.itemUpdate = functions.database.ref('/items/itemId')
          .onUpdate((snap, context) =>
          // Query your users node for matching items and update them
          );





          share|improve this answer













          There isn't a way to do multiple items in the orderByChild, denormalising in NoSQL is fine its just about keeping it in sync like you mention. I would recommend using a Cloud Function which will help you to keep things in sync.



          Another option is to use Firestore instead of the Realtime Database as it has better querying capabilities where you could store a users id in the document and using an array contains filter you could get all the users posts.



          The below is the start of a Cloud function for a realtime database trigger for an update of an item.



          exports.itemUpdate = functions.database.ref('/items/itemId')
          .onUpdate((snap, context) =>
          // Query your users node for matching items and update them
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 16:33









          Jack WoodwardJack Woodward

          63149




          63149












          • Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

            – hugger
            Nov 16 '18 at 2:51











          • There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

            – Jack Woodward
            Nov 16 '18 at 9:17











          • Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

            – hugger
            Nov 16 '18 at 16:38











          • If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

            – Jack Woodward
            Nov 16 '18 at 17:49











          • Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

            – hugger
            Nov 16 '18 at 21:25

















          • Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

            – hugger
            Nov 16 '18 at 2:51











          • There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

            – Jack Woodward
            Nov 16 '18 at 9:17











          • Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

            – hugger
            Nov 16 '18 at 16:38











          • If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

            – Jack Woodward
            Nov 16 '18 at 17:49











          • Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

            – hugger
            Nov 16 '18 at 21:25
















          Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

          – hugger
          Nov 16 '18 at 2:51





          Thanks Jack, this is just what I need. I have tried to accomplish this with cloud functions I'm having issues... I updated my answer with code for this, mind taking a look?

          – hugger
          Nov 16 '18 at 2:51













          There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

          – Jack Woodward
          Nov 16 '18 at 9:17





          There is no .set() parameter on .equalTo() you need to get the results from the query back and then loop through that to update them all. I would suggest getting Visual Studio Code as it will be able to give you the IntelliSense to help you write your code as it would of told you .set() doesnt exist on .equalTo()

          – Jack Woodward
          Nov 16 '18 at 9:17













          Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

          – hugger
          Nov 16 '18 at 16:38





          Ah I see, thank you for this. I have been spending way to much time trying to resolve this... This is my first go at cloud functions - I am still having issues trying to get a simple console log from my forEach. There seems to be not much documentation for this. I updated my question again, I am getting the first console.log but not the one inside the forEach... Is there something I am doing wrong? Cheers.

          – hugger
          Nov 16 '18 at 16:38













          If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

          – Jack Woodward
          Nov 16 '18 at 17:49





          If you use Visual Studio code you will get all the Intellisense to troubleshoot this. You are using snapshot twice so thats not a good practice to shadow variables. and you cant do a forEach on the second snapshot as thats not your data. if you do snapshot.val() then you can access the forEach().

          – Jack Woodward
          Nov 16 '18 at 17:49













          Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

          – hugger
          Nov 16 '18 at 21:25





          Thanks Jack, I am going to have to give visual studio a shot, I'm currently using Atom which displays errors for everything except for cloud functions... I updated my question, I have no clue why this isn't executing. I'm not to sure where I'm going wrong?

          – hugger
          Nov 16 '18 at 21:25



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323729%2ffirebase-cloud-function-not-updating-record%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto