Create a Firebase Cloud Function with Dynamic Parameters

Custom Code

I'm working with Firebase Cloud Functions and I have a function that is scheduled to run periodically. The function is supposed to execute every minute (for the test, but in fact every month) and interact with Firestore to create documents dynamically based on user inputs. However, I'm facing an issue :

Static vs. Dynamic Parameters: I initially created a Cloud Function that works well with static, hardcoded parameters. It runs every minute, retrieves some hardcoded values, and creates documents in Firestore. The function works fine when these values are directly included in the code. The issue arises when I try to replace these static values with dynamic parameters that are supposed to be passed at runtime. The problem is that the function fails to retrieve the data from flutterflow.

Here are parameters that has to be set in flutterflow and then be present in every document it creates periodically :
locatairesPremiumRef: "ducument ref" (a document id from a variable)

loyertotal: "0000" (a number from a variable)

proprietaire: "uid1" (a user id from a variable)

locataire: "uid2" (a user id from a variable)

Here is my actual code :

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const db = admin.firestore();

exports.newloyerdoceachmonth = functions.https.onCall(async (data, context) => {
    // Retrieve dynamic parameters sent from FlutterFlow
    const locatairesPremiumRef = data.locatairespremiumref;
    const loyertotal = data.loyertotal;
    const proprietaire = data.proprietaire;
    const locataire = data.locataire;

    // Check if all parameters are present
    if (!locatairesPremiumRef || !loyertotal || !proprietaire || !locataire) {
        throw new functions.https.HttpsError('invalid-argument', 'All parameters are required');
    }

    // Calculate the month + 2
    const date = new Date();
    date.setMonth(date.getMonth() + 2);
    const moisPlusDeux = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;

    // Create a new document to insert into Firestore
    const newDocumentData = {
        mois: moisPlusDeux,
        locatairespremiumref: locatairesPremiumRef,
        loyertotal: loyertotal,
        proprietaire: proprietaire,
        locataire: locataire
    };

    try {
        // Add data to the Firestore collection
        await db.collection("rents_premium").add(newDocumentData);
        console.log(`Document for month ${moisPlusDeux} created successfully.`);
    } catch (error) {
        console.error("Error creating document:", error);
        throw new functions.https.HttpsError('internal', 'Error adding document');
    }
});
What have you tried so far?

Hours with Chat GPT to try to find the right function

Did you check FlutterFlow's Documentation for this topic?
Yes
1
1 reply