Firebase cloud function won't return anything from forEach?

馋奶兔 提交于 2020-01-06 05:26:16

问题


When my trigger receives an update, I am trying to loop through another node via the admin sdk to iterate through the results from the snapshot with matching keys from the wildcard {itemId}. For some reason my forEach seems to not do anything and I don't know why. I have been trying to resolve this for quite some time now. I have never had a problem querying my firebase DB but now that I am trying to do so in cloud functions I have not been having any luck at all. I don't know if this is related to firebase-admin at all?

Here is my code, I left some comments to show you what I am trying to accomplish more clearly:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

// admin.initializeApp();
//is this enough or do I need to add more configuration???
admin.initializeApp(functions.config().firebase);


exports.itemUpdate = functions.database
  .ref('/items/{itemId}')
  .onUpdate((change, context) => {
    const before = change.before.val();
    const after = change.after.val();

    console.log(after); //data I want to use to update query results


    //if (before === after) {
      //console.log('data didnt change')
      //return null;
    //}

admin.database().ref('users')
  .orderByChild('likedItems')
  .equalTo(context.params.itemId)
  .once('value')
  .then(snapshot => {
    //I am able to log the snapshot, but the data is misleading???
    console.log(snapshot);
    //I am trying to look for the likedItems that match the itemId (wild card)
    //from my query - Why wont anything log? Is is a problem with my admin?
    snapshot.forEach((childSnapshot) => {
      var key = childSnapshot.key;
      var targetItem = context.params.itemId; //item(s) to update
      var child = childSnapshot.child;
      var childData = childSnapshot.val();
      //attempt to update childData that match the targetItem (that was originally updated)
      //targetItem returns undefined here but when i log it its not undefined?

      return childData.likedItems.targetItem.update(after);
  });
    return;
  }).catch(error =>{
    console.log(error);
  });
});

Here is a look at what my DB structure looks like for the items, and then the users likedItems in their node:

    "items" : {
        "Item1" : {
          "title" : "title1",
          "type" : "type1"
        },
        "Item2" : {
          "title" : "title2",
          "type" : "type2"
        },
        "Item3" : {
          "title" : "title3",
          "type" : "type3"
        },
        "Item4" : {
          "title" : "title4",
          "type" : "type4"
        },
        ...
        ...
        ...
      },
      "users" : {
        "EoAhYX3mYcRASr5oPD1eSZ979Vr1" : {
          "followerCount" : 4,
          "followingCount" : 2,
          "likedItems" : {
            "Item1" : {
              "title" : "title1",
              "type" : "type1"
            },
            "Item10" : {
              "title" : "title10",
              "type" : "type10"
            },
            "Item12" : {
              "title" : "title12",
              "type" : "type12"
            }
          },
          "username" : "user1"
        },
        ...
        ...
        ...
    }

I appreciate all the help/guidence I can get!

Cheers.

来源:https://stackoverflow.com/questions/53399903/firebase-cloud-function-wont-return-anything-from-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!