I'm using Firebase v9.0.2
Uncaught TypeError: Cannot read properties of undefined (reading 'pieceNum_')
<-- 4361
--> https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js
CHAT_ROOT is /chats/${chatroomid}.
The value of chatroomid is taken from user input earlier.
import { Database } from 'initAppGlobals.js';
import * as FirebaseDB from 'https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js';
// this code contains nesting of unction calls
FirebaseDB.onValue(
FirebaseDB.limitToLast(1,
FirebaseDB.orderByKey(
FirebaseDB.ref(Database, CHAT_ROOT)
)
),
(snapshot) => {
// Load messages into UI as HTML
}, (error) => {
// display error message
});
The above code works only if I remove both limitToLast and orderByKey functions.
import { Database } from 'initAppGlobals.js';
import * as FirebaseDB from 'https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js';
// this code contains nesting of function calls
FirebaseDB.onValue(
FirebaseDB.ref(Database, CHAT_ROOT),
(snapshot) => {
// Load messages into UI as HTML
}, (error) => {
// display error message
});
But that affects performance as the database size grows.
{
"chats": {
"$chatroomid": {
".read": "auth !== null",
".write": "auth !== null",
".indexOn": ".value"
}
}
}
You're nesting the calls too far, and are not creating a query out of the conditions.
To create a query, you need a reference and then a number of query operations, so:
const query = FirebaseDB.query(
FirebaseDB.ref(Database, CHAT_ROOT),
FirebaseDB.limitToLast(1),
FirebaseDB.orderByKey()
);
Then you can execute the query with:
FirebaseDB.onValue(query, (snapshot) => {
// Load messages into UI as HTML
}, (error) => {
// display error message
});