2

I have the following structure:

{
  "Campaign" : {
    "-KtghP_NMOFrjN_RrI6f" : {
      "Projects" : {
        "-Kz5g4j8dKgxtqQsPfN1" : {
          "createByName" : "Michal",
          "profileId" : "-KtlDwI3Bq4Bi7R23kya"
        },
        "-KzGCaLblTxzu4Nje15Z" : {
          "createByName" : "Roy",
          "profileId" : "-Kxx_egu9h4GOrxqM1nB"
        }
      }
    },
    "-KyjE0HPNSg27Kpurq8l" : {
      "Projects" : {
        "-KzBUZBsI947HckV296O" : {
          "createByName" : "Roy",
          "profileId" : "-Kxx_egu9h4GOrxqM1nB"
        }
      }
    }
  },
  "UserProfile" : {
    "-Kxx_egu9h4GOrxqM1nB" : {
      "MyProjects" : {
        "-KzGC3Yn4bAAorcwDhUT" : {
          "CampaignId" : "-KyjE0HPNSg27Kpurq8l",
          "projectId" : "-KzBUZBsI947HckV296O"
        },
        "-KzGCaeTqMm_g1Jq6u6i" : {
          "CampaignId" : "-KtghP_NMOFrjN_RrI6f",
          "projectId" : "-KzGCaLblTxzu4Nje15Z"
        }
      },
      "firstName" : "roy"
    }
  }
}

i have the profile id.

I need to get the different projects of the user, under the different Campaigns..

I want to get the list of MyProjects by the userid, and then iterate (with the keys i got from MyProjects) over the campaigns -(key)-> projects -(key)-> profileId and compare them... (hope it was clear enough...)

HOW DO I DO THAT?

Ok, after some more research, i went a different way. it seems like i don't need "MyProjects".

var userId = "-Kxx_egu9h4GOrxqM1nB";
var campaignRef = this.db.app.database().ref('Campaign'); //root
var projectsRef = campaignRef.child('Projects');

projectsRef.orderByChild('profileId').equalTo(userId).once("value", (snap) => {
  console.log(snap.val());
});

but still i get NULL.. (tried "child_added" and .on in different combinations - nothing...)

2
  • 2
    Please show us what you tried so we can help you
    – Melchia
    Commented Nov 20, 2017 at 1:32
  • Thank you for your comment. added my code..
    – Lerman
    Commented Nov 20, 2017 at 23:56

1 Answer 1

0

After logging in, and pulling the Campaign object, you can try running the following code:

// user is an object from the userProfile. You can also just directly access the object and get what you need. Really, it's up to you. I'll assume that you also stored the user ID (uid) here too.
var user = {},
  campaigns = {},
  uid = {},
  myProjects = [];

function getProjects() {
  for (var i in campaigns) {
    for (var k in campaigns[i]) {
      // Make sure that the profileId of the project and the uid match, then push object into myProjects array
      campaigns[i][k].profileId === uid ? myProjects.push(campaigns[i][k]) : 'nada';
    }
  }
}

In the myProjects array will you find your projects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.