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

152
Visualizações
Firebase RTDB: How exactly the 2nd argument of cursors(startAt, endAt...) work?

code:

import {
  orderByValue,
  ref,
  getDatabase,
  set,
  get,
  query,
  startAfter
} from "firebase/database";
import { initializeApp } from "firebase/app";

const app = initializeApp({
  projectId: "some-id"
});

const db = getDatabase();

const testRun = async () => {
  await set(ref(db), { d: 5, y: 4, b: 6, m: 0, p: 1 });
  const snapshot = await get(
    query(ref(db), orderByValue(), startAfter(0, "b"))
  );
  console.log(snapshot.val());
};

testRun();

result:{b: 6, d: 5, m: 0, p: 1, y: 4}

how is this make sense? with startAfter(0, 'b'), I was expecting the result to be {d: 5, p: 1, y: 4}, but that is not totally the case.

what make thing even confusing is, the result includes m:0!!

however if I go with startAfter(0), the result is {b: 6, d: 5, p: 1, y: 4} which is expected.

how exactly does the 2nd argument work?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The second value you specify to startAfter (and its sibling methods) is typically used as a so-called disambiguation key, in case there are multiple child nodes with the value that you pass in the first argument. It is only applied after the filter on the regular value has been executed.

When we order your data by value, we get:

{ 
  m: 0, 
  p: 1,
  y: 4, 
  d: 5, 
  b: 6 
}

Since there's only one node with value 0, the database returns all nodes after that one.

about 4 years ago · Juan Pablo Isaza Relatório

0

Ok, I think I understand how it works

first we order according to the orderBy clause, in this case we order by value

{ 
  m: 0, 
  p: 1,
  y: 4, 
  d: 5, 
  b: 6 
}

to understand what startAfter(0,'b') return, we simply imagine where b:0 would be located

{ 
  b: 0, // imagine
  m: 0, 
  p: 1,
  y: 4, 
  d: 5, 
  b: 6 
}

since b come before m, so everything after b:0 is what we will get, which is everything

with the same logic, we can deduce what endAt(6, 'd') return

{ 
  m: 0, 
  p: 1,
  y: 4, 
  d: 5, 
  b: 6 
  d: 6, // imagine
}

the result would be also everything

the trick is to know where the reference point is

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