Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda