Skip to content

Instantly share code, notes, and snippets.

@bacongravy
Created June 8, 2020 05:59
Show Gist options
  • Select an option

  • Save bacongravy/d790a33f0f06244cb822d9551d593cff to your computer and use it in GitHub Desktop.

Select an option

Save bacongravy/d790a33f0f06244cb822d9551d593cff to your computer and use it in GitHub Desktop.
const { Firestore, FieldValue } = require("@google-cloud/firestore");
const credentials = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
const db = new Firestore({ projectId: credentials.project_id, credentials });
const docRef = db.collection("counters").doc("visitors");
docRef.set({}, { merge: true })
const incrementAndGetVisitorCount = async () =>
docRef
.update({ count: FieldValue.increment(1) })
.then(() => docRef.get())
.then(doc => doc.data().count);
app.get("/count", async (request, response) => {
incrementAndGetVisitorCount().then(count => {
response.json(count);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment