Firebase Cloud Functions Params Validation and Error Handling (Node.js)

Feb 23, 2021

NOTE: Sample Firebase Cloud Functions (Node.js).

const functions = require('firebase-functions');const admin = require('firebase-admin');const app = !admin.apps.length ? admin.initializeApp() : admin.app();const db = admin.firestore();exports.deleteBot = functions.https.onCall(async (data, context) => {  const uid = context.auth.uid  if (!context.auth) {    throw new functions.https.HttpsError("unauthenticated", "Request had invalid credentials.")    // throw new functions.https.HttpsError('failed-precondition', 'Credentials are missing')  }  const botId = data.botId  if (!botId) {    throw new functions.https.HttpsError('invalid-argument', 'Missing botId');  }  const userRef = db.collection('user').doc(uid)  const botRef = userRef.collection('bot').doc(botId)  const batch = db.batch()  batch.delete(botRef)    const botMetaRef = userRef.collection('bot_meta').doc(botId)  batch.delete(botMetaRef)    await batch.commit();  return {    uid: uid,    botId: data.botId,    status: 'ok'  }});

NOTE: Call Cloud Functions From Web/JavaScript

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.