1

I have the following structure:

enter image description here

i have a profile id, and i want to retrieve all projects that contains that profileId.

this is my code:

var campaignRef = this.db.app.database().ref('Campaign');
var projectsRef = campaignRef.child('Projects');

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

the "userId" is valid and existing but i just get null...

and how can i return a list of "FirebaseListObservable" containing those projects.. (or a snapshot of them)

Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lerman
  • 135
  • 2
  • 9
  • 2
    Take a look at this https://stackoverflow.com/questions/40713137/querying-below-autoids-in-firebase I don't think you can query below two autoId's so you might need to redesign your data structure. You have projectRef as Campaign -> Projects, but there is an autoId between them so I think that's your issue. You can query below an autoID using orderby. However, in your case you're are going below two id's and I'm not sure that's possible. – DoesData Nov 21 '17 at 16:03
  • 1
    As @DoesData says, this type of query is not possible. You'll need to denormalize your data. For an example, see my answer here: https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Nov 21 '17 at 16:23
  • Well, ok then.. since it doesn't work, i'll have to take your word for it. but, how else would you have done it?... – Lerman Nov 21 '17 at 19:42
  • 1
    Why do you have an autoId below campaign? Don't you know the name of the campaign? If you did then you could append that name to your query and achieve your desired results. Otherwise you need to restructure the data. Something like pulling projects out into it's own tree might work for you. It's hard to provide direct advice without knowing your specific use cases. – DoesData Nov 21 '17 at 19:48
  • the name of the campaign isn't unique. "Something like pulling projects out into it's own tree might work for you" - that's what i did... Thank you for your help! – Lerman Nov 22 '17 at 17:09

0 Answers0