Home > Enterprise >  Is there anyway i can update a firestore document automatically even if app is closed, is there a li
Is there anyway i can update a firestore document automatically even if app is closed, is there a li

Time:12-26

i have a firestore and project that needs to be updated automatically without user interaction but i do not know how to go about it, any help would be appreciated. take a look at the json to understand better

const party =  {
 id: 'bgvrfhbhgnhs',
  isPrivate: 'true',
  isStarted: false,
  created_At: '2021-12-26T05:20:29.000Z',
   start_date: '2021-12-26T02:00:56.000Z'
   
}

I want to update the isStarted field to true once the current time is equal to start_date

CodePudding user response:

I think you will need Firebase Cloud Function, although I don't understand exactly what you mean. With Cloud Functions, you can automatically run (add, delete, update, everything) codes on Google's Servers without the need for application and user interaction. For example, in accordance with your example, it can automatically set "isStarted" to true when it hits the "start_date" time. If you want to code a system that does not require user interaction and should work automatically, you should definitely use Cloud Functions. Otherwise, you cannot do this on the application side.

For more info visit Cloud Functions

CodePudding user response:

Ok, I managed to find a workaround to updating my documents automatically without user interaction since the google billing service won’t accept my card to enable cloud functions for my project, I tried what I could to make my code work and I don’t know if other peeps would follow my idea or if my idea would solve similar issues.

What I did was that in my nextjs file I created an API endpoint to fetch and update documents after installing the firebase admin SDK, so I fetched all documents and converted all start_date fields in each document to time and checked for documents whose start date is less than or equal to current date, so after getting the document, I ran a firestore function to Update the document.

Tho this will only run when you make a request to my domain.com/api/update-parties and never run again

In other to make it run at scheduled intervals, I signed up for a free tier account at https://www.easycron.com and added my API endpoint to EASYCRON to make requests to my endpoint at a minute interval, so when the request hits my endpoint, it runs my code like other serverless functions

  • Related