Home > Software engineering >  Why did firestore move from firestore.collection() to collection(firestore)?
Why did firestore move from firestore.collection() to collection(firestore)?

Time:01-16

It used to be

db.collection("customersData").add({
      name: customerName,
      password: customerPassword,
    });

Now it is

async function getCities(db) {
  const citiesCol = collection(db, 'cities');
  const citySnapshot = await getDocs(citiesCol);
  const cityList = citySnapshot.docs.map(doc => doc.data());
  return cityList;
}

And you have to import the methods seperately import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';.

What was the reason for this?
Is it possible to use the old way of writing still?

CodePudding user response:

What was the reason for this?

You can read about it in the release notes for version 9.

This release introduces the new modular API, which enables tree-shaking, bundle size reduction, and other benefits. See SDK versions 8 and 9 for more details.

Is it possible to use the old way of writing still?

Yes, you can use any prior version of the SDK before the changes in 9.

  •  Tags:  
  • Related