Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Firebase RTDB limitToLast(n, orderByKey(ref)) causes TypeError: Cannot read properties of undefined (reading 'pieceNum_')

I'm using Firebase v9.0.2

Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'pieceNum_')

<-- 4361

--> https://www.gstatic.com/firebasejs/9.0.2/firebase-database.js

Code variables:

CHAT_ROOT is /chats/${chatroomid}. The value of chatroomid is taken from user input earlier.

Code:

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
});

Possible issue

The above code works only if I remove both limitToLast and orderByKey functions.

So, this works:

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.

Database rules:

{
  "chats": {
    "$chatroomid": {
      ".read": "auth !== null",
      ".write": "auth !== null",
      ".indexOn": ".value"
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!