How to Use Firestore with Node.js: Simple Guide
To use
Firestore with Node.js, install the @google-cloud/firestore package, initialize a Firestore client with your project ID, and then use the client to read or write data. This setup lets your Node.js app interact with Firestore's NoSQL database easily.Syntax
First, install the Firestore library. Then, import and create a Firestore client by specifying your Google Cloud project ID. Use this client to access collections and documents.
- Import Firestore: Load the Firestore library.
- Initialize client: Connect to your Firestore database.
- Access data: Use methods like
collection(),doc(),get(), andset()to read and write.
javascript
const { Firestore } = require('@google-cloud/firestore'); async function main() { const firestore = new Firestore({ projectId: 'your-project-id', }); // Access a collection const usersCollection = firestore.collection('users'); // Access a document const userDoc = usersCollection.doc('user-id'); // Read document data const snapshot = await userDoc.get(); if (snapshot.exists) { console.log(snapshot.data()); } // Write document data await userDoc.set({ name: 'Alice', age: 30 }); } main().catch(console.error);
Example
This example shows how to connect to Firestore, add a new user document, and then read it back to print the data.
javascript
const { Firestore } = require('@google-cloud/firestore'); async function runExample() { const firestore = new Firestore({ projectId: 'your-project-id', }); const userRef = firestore.collection('users').doc('user1'); // Write data await userRef.set({ name: 'Alice', age: 30 }); // Read data const doc = await userRef.get(); if (doc.exists) { console.log('User data:', doc.data()); } else { console.log('No such document!'); } } runExample().catch(console.error);
Output
User data: { name: 'Alice', age: 30 }
Common Pitfalls
Common mistakes include:
- Not installing
@google-cloud/firestorebefore use. - Forgetting to set the correct
projectIdor authentication credentials. - Using
awaitwithoutasyncfunctions causing errors. - Trying to read a document that does not exist without checking
exists.
Always handle promises properly and check if documents exist before accessing data.
javascript
/* Wrong: Missing async and await */ async function wrongExample() { const doc = firestore.collection('users').doc('user1').get(); console.log((await doc).data()); // Error: doc is a Promise if not awaited } /* Right: Use async/await */ async function readDoc() { const doc = await firestore.collection('users').doc('user1').get(); if (doc.exists) { console.log(doc.data()); } else { console.log('No document found'); } }
Quick Reference
Key Firestore methods for Node.js:
| Method | Description |
|---|---|
| Firestore() | Create a Firestore client instance |
| collection(name) | Access a collection by name |
| doc(id) | Access a document by ID |
| get() | Read data from a document or collection |
| set(data) | Write data to a document |
| update(data) | Update fields in a document |
| delete() | Delete a document |
Key Takeaways
Install and import @google-cloud/firestore to use Firestore in Node.js.
Initialize Firestore with your Google Cloud project ID before accessing data.
Use async/await to handle Firestore operations and check if documents exist.
Common errors come from missing async or incorrect project setup.
Use Firestore methods like collection(), doc(), get(), and set() to manage data.