DEV Community

Brijrajsinh parmar
Brijrajsinh parmar

Posted on

1

Firebase v8 vs v9 Queries

onSnapshot

v8

unsub = firestore
.collection("chats")
      .where("roomid", "==", roomid)
      .orderBy("timestamp", "desc")
      .onSnapshot((snapshot) => {
        let docs = [];
        snapshot.docs.forEach((each) => {
          docs.push({ ...each.data(), id: each.id });
        });

        setChats(docs);
        setLoading(false);
      });
    return () => unsub();

Enter fullscreen mode Exit fullscreen mode

v9

unsub = onSnapshot(query(collection(firestore, 
 "projects"),where("completed", "==", true)),
 (doc) => {
  let data = [];
  doc.forEach((each) => {
   data.push({ ...each.data(), uid: each.id });
  });
  setProjects([...data]);
  setLoading(false);
  data = [];
});
Enter fullscreen mode Exit fullscreen mode

insert

v8

const ref = firestore.collection("chats");
await ref.doc().set(newDoc);
Enter fullscreen mode Exit fullscreen mode

v9

await addDoc(collection(firestore, "projects"), projectDetails)
Enter fullscreen mode Exit fullscreen mode

update

v8

const query = firestore.collection("Lopn").doc(user.uid);
query.update(doc);
Enter fullscreen mode Exit fullscreen mode

v9

const ref = doc(firestore, "projects", pid);
await updateDoc(ref, data);
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more