want to create a scheduled cloud function to create document every 31 days from when cloud function action defined and only to the subcollection which i give reference with variable

Custom Code

to create document every 31 days from when cloud function action defined and only to the subcollection which i give reference with variable createpageA(in image).

in this i am creating new document in collection(Newvehicle)(its output variable is 'createpageA') simultaneously also in subcollection with reference createpageA then navigating to the page containing data of subcollection.

now what i want is, my backend firestore to create document every 31 days in subcollection with createpageA reference with fields i will chose.

i don't know coding much, i tried chatgpt to give me code and i used it but while test it says it did it but in firestore document was not created and in function dashboard also there was no request taken by firebase(for test i did that in duration of 1 minute)

can anyone give me code

this is the code i used:

const functions = require('firebase-functions');

  1. const admin = require('firebase-admin');

  2. // To avoid deployment errors, do not call admin.initializeApp() in your code

  3. exports.createPeriodicTransaction = functions.region('us-central1').https.onCall(

  4.   (data, context) => {

  5.     // Write your code below!

  6. const functions = require('firebase-functions');

  7. const admin = require('firebase-admin');

  8. admin.initializeApp();

  9. exports.createPeriodicTransaction = functions.pubsub

  10.   .schedule('every 1 minutes')

  11.   .onRun(async (context) => {

  12.     try {

  13.       // Use the createpageA variable as the document reference

  14.       const createpageA = context.params.createpageA;

  15.       if (!createpageA) {

  16.         console.error('The variable createpageA is not provided.');

  17.         return null;

  18.       }

  19.       const db = admin.firestore();

  20.       // Reference the document in the Newvehicle collection

  21.       const docRef = db.collection('Newvehicle').doc(createpageA);

  22.       const docSnapshot = await docRef.get();

  23.       if (!docSnapshot.exists) {

  24.         console.error(`Document with ID ${createpageA} does not exist.`);

  25.         return null;

  26.       }

  27.       // Get the emi field value from the document

  28.       const docData = docSnapshot.data();

  29.       const emiValue = docData.emi;

  30.       if (!emiValue) {

  31.         console.error(`EMI field is missing for document ${createpageA}.`);

  32.         return null;

  33.       }

  34.       // Add a new document in the CustomertransactionN subcollection

  35.       await docRef.collection('CustomertransactionN').add({

  36.         date: admin.firestore.Timestamp.now(),

  37.         type: 'Due',

  38.         due_amount: emiValue,

  39.       });

  40.       console.log(`Transaction created successfully for document ${createpageA}.`);

  41.       return null;

  42.     } catch (error) {

  43.       console.error('Error creating transaction:', error);

  44.       throw new functions.https.HttpsError(

  45.         'internal',

  46.         'Failed to create transaction document.'

  47.       );

  48.     }

  49.   });

  50.     // Write your code above!

  51.   }

  52. );

What have you tried so far?

i tried flutterflow documentation, tutorial videos of cloud function and chatgpt for help to write code

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