paj-co
paj-co

Reputation: 138

Firebase Cloud Functions with TypeScript: Realtime Database update ends with success but not updates anything, JS works fine

I added Cloud Functions to Firebase project with Firebase CLI. I have started with JavaScript, managed to write working code.

Then I decided to switch to TypeScript. So I decided to delete all Cloud Functions JS related files and start with firebase init cmd from scratch. I copied code from index.js to index.ts, needed only to change how dataMap Map was declared.

So, now whenever I look into console logs on Firebase, everything seems to work fine, everything is logged out, and I'm getting success message on client side.

However nothing happens in Realtime Database, no update, no trace of any data.

I know almost nothing about JS / TS, so every suggestion about code and solution is welcomed.

I'm using node: 14.17.6 and updated firebase-tools to 9.18.0, firebase-admin to 9.11.1 and firebase-functions to 3.15.6.

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();

exports.createItemWithVIP = functions.region("europe-west1").
    https.onCall((data, context) => {
      // Checking that the user is authenticated.
      if (!context.auth) {
        console.log("Unauthenticated request");
        throw new functions.https.HttpsError("permission-denied", "You have" +
          " no permission to perform this operation");
      }

      // item details
      const foo = data.foo;
      const bar = data.bar;
      console.log(`foo: ${foo}, bar: ${bar}`);

      const userUID = context.auth.uid;
      console.log(`userUID: ${userUID}`);

      const db = admin.database();
      // get new item uid
      const somePathTableReference = db.ref("somePath/").push();
      const itemUID = `${somePathTableReference}`
          .split("somePath/")[1];
      console.log(`itemUID: ${itemUID}`);

      const itemPath = `somePath/${itemUID}`;
      const userPath = `users/${userUID}/somePath/${itemUID}`;

      const dataMap = new Map<string, unknown>();
      dataMap.set(`${itemPath}/vip/${userUID}`, true);
      dataMap.set(`${itemPath}/foo`, foo);
      dataMap.set(`${itemPath}/bar`, bar);
      dataMap.set(`${userPath}/role`, "vip");
      dataMap.set(`${userPath}/foo`, foo);
      dataMap.set(`${userPath}bar`, bar);

      dataMap.forEach((value: unknown, key: string) =>
        console.log(key, value));

      return db.ref("/").update(dataMap).then(() => {
        console.log(`Added new item with key: ${itemUID} ` +
          `; added vip role to user ${userUID}`);
        return {"message": "Success"};
      }).catch((e) => {
        console.log(`Error: ${e}`);
        throw new functions.https.HttpsError("unknown", "Unknown error" +
          "occured");
      });
    });


Upvotes: 2

Views: 763

Answers (2)

Loscho19
Loscho19

Reputation: 43

This works for me.

const dMap: { [key: string]: any; } = {};

dMap[`${itemPath}/vip/${userUID}`]= true;
dMap[`${itemPath}/foo`]= foo;
dMap[`${itemPath}/bar`]= bar;
dMap[`${userPath}/role`]= "vip";
dMap[`${userPath}/foo`]= foo;
dMap[`${userPath}bar`]= bar;

db.ref().update(dMap);

Upvotes: 0

Dharmaraj
Dharmaraj

Reputation: 50830

I'm not totally sure about the reason but updating with an object directly instead of new Map() seems to be working (and yes, it didn't work for me with a Map):

const dMap = {
  [`${itemPath}/vip/${userUID}`]: true,
  [`${itemPath}/foo`]: foo,
  [`${itemPath}/bar`]: bar,
  [`${userPath}/role`]: "vip",
  [`${userPath}/foo`]: foo,
  [`${userPath}bar`]: bar
}

try {
  await db.ref("/").update(dMap);
  console.log(`Added new item with key: ${itemUID}; added vip role to user ${userUID}`);

  return {"message": "Success"};
} catch (e) {
  console.log(`Error: ${e}`);
  throw new functions.https.HttpsError("unknown", "Unknown error" +
    "occured");
}

Upvotes: 2

Related Questions