Home > database >  How to access Firebase double nested object properties?
How to access Firebase double nested object properties?

Time:12-04

I am trying to get all users that have a certain listId in their contactLists object. My firebase realtime database structure is as follows:

user:  
  john:
     contactLists: 
       -NIOsvb: true,
     

I have other users in the DB and I want to get all that have -NIOsvb in their contactLists object. This is the approach I tried (listId is passed as a parameter):

const snapshot = await get(query(ref(db, "users"), orderByChild("contactLists"), equalTo(listId)))

I expected to get all the user objects that have this id in their contactLists. However, the value of snapshot is null. Any suggestions would be appreciated, as I don't have a lot of experience with Firebase functions.

CodePudding user response:

To get all users that have a certain listId in their contactLists object, you can use the orderByChild() and equalTo() methods of the Firebase Realtime Database query object. Your code should look something like this:

const snapshot = await get(query(ref(db, "users"), orderByChild("contactLists"), equalTo(listId)))

This code will retrieve a snapshot of the data at the users node in the database, and will only include users that have the listId in their contactLists object.

Note that the orderByChild() and equalTo() methods expect the name of the property to query as a string, and the value to match as a value. In your code, you are using orderByChild("contactLists"), which is trying to query a property called contactLists on each user object. However, this property is actually an object, not a property. To query the -NIOsvb property of the contactLists object, you should use the following code instead:

const snapshot = await get(query(ref(db, "users"), orderByChild("contactLists/-NIOsvb"), equalTo(true)))

This will query the -NIOsvb property of the contactLists object, and will only return users that have a value of true for this property.

It's also worth noting that, since you are querying the database for a specific value, you don't need to use the orderByChild() method. You can simply use the equalTo() method on its own, like this:

const snapshot = await get(query(ref(db, "users"), equalTo(true), "contactLists/-NIOsvb"))

This code will have the same effect as the previous example, but is simpler and more straightforward.

CodePudding user response:

It looks like there are a few issues with the query you are using to get the users with a specific listId in their contactLists. Here are a few suggestions to help you fix the query:

  • The orderByChild method should be used with a property name, not with a whole object. In your case, you can use orderByChild("contactLists.-NIOsvb") to order the users by the -NIOsvb property in their contactLists objects.
  • The equalTo method should be used with a value, not with a whole object. In your case, you can use equalTo(true) to match users that have a -NIOsvb property with the value true in their contactLists objects.
  • When you use the orderByChild and equalTo methods, you need to specify the property and value you want to query for in the correct order. In your case, you can use orderByChild("contactLists.-NIOsvb").equalTo(true) to query for users that have a -NIOsvb property with the value true in their contactLists objects.

Here is an example of how you can use these methods to fix your query:

   const snapshot = await get(
  query(
    ref(db, "users"),
    orderByChild("contactLists.-NIOsvb").equalTo(true)
  )
);

This query will select all the documents from the users collection where the contactLists.-NIOsvb field has the value true. The query will return a QuerySnapshot object, which you can iterate over to access the matching documents.

  • Related