OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Firebase Realtime Database .indexOn Multiple Level Dynamic key

  • Thread starter Thread starter Ugur Kellecioglu
  • Start date Start date
U

Ugur Kellecioglu

Guest
I have a chat app where I create new keys dynamically. Within each dynamic key, when a user sends a message, I create an object inside that key with its own dynamic key.

The structure is as follows:

Code:
https://xxxxx-default-rtdb.europe-west1.firebasedatabase.app/
├── chrome%3A%2F%2Fextensions%2F -> dynamically created
    ├── -O-VkRKionPx9eGQs9fs -> dynamically created
        ├── createdAt: 1718536488277
        ├── name: "uur37484225"
        ├── text: "dsd"
        ├── uid: "itZx6OYhT9QqZHkGiZ29cvUEard2"

The following rules configuration doesn't seem to be working:

Code:
{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "$url": {
      "$id": {
        ".indexOn": ["createdAt"]
      }
    }
  }
}

This is how I call the data:

Code:
const dbRef = ref(db, encodeFirebaseKey(currentUrl))
let queryRef

if (lastCreatedAt) {
  queryRef = query(dbRef, orderByChild("createdAt"), limitToLast(20))
} else {
  queryRef = query(dbRef, orderByChild("createdAt"), limitToLast(20))
}

get(queryRef)
  .then((snapshot) => {
    // Handle the snapshot
  })
  .catch((error) => {
    console.error("Error fetching messages:", error)
  })

The error I'm getting is:

Error: Index not defined, add ".indexOn": "createdAt", for path "/chrome%3A%2F%2Fextensions%2F", to the rules

What seems to be problem? I couldn't really find any documentation about this issue

<p>I have a chat app where I create new keys dynamically. Within each dynamic key, when a user sends a message, I create an object inside that key with its own dynamic key.</p>
<p>The structure is as follows:</p>
<pre><code>https://xxxxx-default-rtdb.europe-west1.firebasedatabase.app/
├── chrome%3A%2F%2Fextensions%2F -> dynamically created
├── -O-VkRKionPx9eGQs9fs -> dynamically created
├── createdAt: 1718536488277
├── name: "uur37484225"
├── text: "dsd"
├── uid: "itZx6OYhT9QqZHkGiZ29cvUEard2"
</code></pre>
<p>The following rules configuration doesn't seem to be working:</p>
<pre><code>{
"rules": {
".read": "auth != null",
".write": "auth != null",
"$url": {
"$id": {
".indexOn": ["createdAt"]
}
}
}
}
</code></pre>
<p>This is how I call the data:</p>
<pre><code>const dbRef = ref(db, encodeFirebaseKey(currentUrl))
let queryRef

if (lastCreatedAt) {
queryRef = query(dbRef, orderByChild("createdAt"), limitToLast(20))
} else {
queryRef = query(dbRef, orderByChild("createdAt"), limitToLast(20))
}

get(queryRef)
.then((snapshot) => {
// Handle the snapshot
})
.catch((error) => {
console.error("Error fetching messages:", error)
})
</code></pre>
<p>The error I'm getting is:</p>
<blockquote>
<p>Error: Index not defined, add ".indexOn": "createdAt", for path "/chrome%3A%2F%2Fextensions%2F", to the rules</p>
</blockquote>
<p>What seems to be problem? I couldn't really find any documentation about this issue</p>
 
Top