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

207
Views
Combinar limitToFirst() y limitToLast() conduce a un comportamiento inesperado

Tengo el siguiente conjunto de datos en una base de datos de Firebase en tiempo real:

 { "posts": { "key1": { message: "message", title: "title", timeStamp: 1513160947658 }, "key2": { message: "message", title: "title", timeStamp: 1625904019915 }, "key3": { message: "message", title: "title", timeStamp: 1626171405405 } } }

Aquí está el componente Firebase:

Firebase.js

 import { get, getDatabase, limitToFirst, limitToLast, onValue, orderByChild, query, ref } from 'firebase/database' import firebase from 'firebase/compat/app' import 'firebase/compat/database' const config = { ... } const db = getDatabase() firebase.initializeApp(config) export { db, get, limitToFirst, limitToLast, onValue, orderByChild, query, ref }

Un componente obtiene la primera y la última publicación:

FirstAndLast.js

 import { db, get, limitToFirst, limitToLast, orderByChild, query, ref } from './Firebase' const fetch = async () => { // If I execute both lines, Listener.js snapshot.val() returns a null value // If I remove one of these lines, Listener.js snapshot.val() returns the desired object let snapshotFirst = await get(query(ref(db, 'posts'), orderByChild('timeStamp'), limitToFirst(1))) let snapshotLast = await get(query(ref(db, 'posts'), orderByChild('timeStamp'), limitToLast(1))) }

Otro componente escucha la rama key2 :

Listener.js

 import { db, onValue, ref } from './Firebase' onValue(ref(db, 'posts/key2'), snapshot => { console.log(snapshot.val()) })

Si ejecuto ambos componentes al mismo tiempo, Listener.js imprime el objeto deseado al principio, pero luego devuelve un valor null como resultado final. Si elimino (o comento) una de las instantáneas en FirstAndLast.js , no hay ningún problema.

¿Hay algo de malo en combinar limitToFirst() y limitToLast() ?

about 4 years ago · Juan Pablo Isaza
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!