I just got started with Firebase Cloud Functions. I’m just trying to update a document field on my Firestore Database through an https triggered function.
Here is my code:
import * as v2 from "firebase-functions/v2";
import * as admin from "firebase-admin";
import {Request, Response} from "express";
admin.initializeApp();
export const helloworld = v2.https
.onRequest(async (request: Request, response: Response) => {
if (request.method === "GET") {
const firstName = request.query.firstName;
if (!firstName) {
response
.status(400)
.send(""firstName" missing in query string.");
}
const db = admin.firestore();
try {
await db.collection("users").doc("***@gmail.com").update({
firstName: firstName,
});
response
.status(200)
.send(""firstName" field correctly updated.");
} catch (error) {
console.error("Error during updating field:", error);
response
.status(500)
.send("Error during field update.");
}
} else {
response
.status(405)
.send("This function only accept GET method.");
}
});
I run this with npm run serve
on my terminal, which should launch npm run build && firebase emulators:start --only functions
, and everything works fine on the online Firestore Database. The problem is that my document on the Emulator doesn’t get affected by the function. What am I doing wrong?