By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Prevent firestore onUpdate cloud functions when onSnapshot is used
JavaScript

Javascript – Prevent firestore onUpdate cloud functions when onSnapshot is used

Admin
Last updated: 2024/02/07 at 5:23 PM
Admin
Share
2 Min Read
Prevent firestore onUpdate cloud functions when onSnapshot is used

Problem:

I got a question to onSnapshot and cloud functions behaviour.

Contents
Problem:Solution:

If I use onSnapshot, as far as I understand, it does most locally and then updates with the cloud every like 1 sec with a hook.
But I have a cloud function functions.firestore.document(…).onUpdate and this one will be called every time of those update hooks if I have like a text field. The function itself is there to check if a type field (typeA,typeB) has been changed, and if so it does some stuff in other collections.

can I trigger the function somehow manually, like when the subsription is unsubsribed for example, that would be the best? but then I cant compare using the snap.before/after functionality in the cloud functions?

what would be a good way to handle this?

Here is my cloud function, it updates the parent document if the type field is changed in the updated document. And I don’t want to have tons of invocations on OnSnapshot updates

export const incrementCountsOnUpdate = functions.firestore
  .document('parent/{parentId}/chiild/{chiildId}')
  .onUpdate(async (snap, context) => {
    const beforeType = snap.before.data().type
    const afterType = snap.after.data().type
    if (beforeType === afterType) return
    const setRef = snap.before.ref.parent.parent
    if (setRef) {
      await setRef.update({
        [getCounter(beforeType)]: FieldValue.increment(-1),
        [getCounter(afterType)]: FieldValue.increment(1)
      })
    }
  })

Solution:

can I trigger the function somehow manually, like when the subsription is unsubsribed for example, that would be the best?

No, you can’t trigger it manually.

what would be a good way to handle this?

You can write another HTTP type function and invoke it from your app whenever you want.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How can I get my HTML button to hide until called again? I have a clock with a button that needs to disappear when clicked or at a certain time Javascript – How can I get my HTML button to hide until called again? I have a clock with a button that needs to disappear when clicked or at a certain time
Next Article JavaScript progress bar unhides but won't update Javascript – JavaScript progress bar unhides but won’t update
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?