Strategies for retrieving data from a Collection document during an onUpdate event

I've encountered an issue while trying to fetch data from a collection called "users" in Firebase onUpdate of a document named "bulkMsgs". Each time I attempt this, I receive an error message saying "Error getting user". Both methods work perfectly fine on their own, but when combined, the error occurs.

What could possibly be causing this error?

var functions = require("firebase-functions");
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.bulkMsg = functions.firestore
    .document('/bulkMsgs/{bulkMsgsId}').onUpdate((snap, context) => {

        const newValue = snap.after.data();
        const regDate = newValue.regDate;

        admin.firestore().collection('users').get().then(onUser => {
            onUser.forEach(oneUser => {
                if (oneUser.data().notification === "true") {
                   console.log(oneUser.id);
                }
            });
            return true;

        }).catch(err => {
            console.log('Error getting user', err);
        });


        return true;
    });

View Image showing the error in the Cloud Function log.

Answer №1

Encountering an Error: 4 DEADLINE_EXCEEDED? A brief search online reveals it may be due to requests taking too long.

Despite the input you provided in the comments, it doesn't appear you're handling large quantities.

Have you tried returning the promise from the asynchronous get() method like this:

exports.bulkMsg = functions.firestore
    .document('/bulkMsgs/{bulkMsgsId}').onUpdate((snap, context) => {

        const newValue = snap.after.data();
        const regDate = newValue.regDate;

        return admin.firestore().collection('users').get()
        .then(onUser => {
            onUser.forEach(oneUser => {
                if (oneUser.data().notification === "true") {
                   console.log(oneUser.id);
                }
            });
            return null;
        }).catch(err => {
            console.log('Error getting user', err);
            return null;
        });

    });

In a background-triggered Cloud Function, it's crucial to return a Promise or value. I recommend watching the 3 videos on "JavaScript Promises" in the Firebase video series: https://firebase.google.com/docs/functions/video-series/ for more insight on this essential topic.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to inject basic service into component

Despite all my efforts, I am struggling to inject a simple service into an Angular2 component. Everything is transpiling correctly, but I keep encountering this error: EXCEPTION: TypeError: Cannot read property 'getSurveyItem' of undefined Even ...

What is the best way to utilize the characteristics found in the properties of a different type?

Consider the following scenario: type Individual = { name: string, age: number, nicknames: string[], } Now, I aim to generate a new type based on the types of the properties within the Individual type. The desired outcome is: type IndividualTypes = ...

Is there a way to make the vss sdk treeview collapse automatically?

Is there a way to have the nodes of a combo control of type TreeView.ComboTreeBehaviorName collapsed by default? I've searched through the documentation at this link and this link, but couldn't find a solution. I also examined the types in vss. ...

Error: The checkbox was clicked, but an undefined property (includes) cannot be read

Link to live project preview on CodeSandbox Visit the product page with checkbox I have developed a code snippet that allows users to filter products by checking a box labeled "Show Consignment Products Only", displaying only those products with the term ...

What is the best way to retrieve all designs from CouchDB?

I have been working on my application using a combination of CouchDB and Angular technologies. To retrieve all documents, I have implemented the following function: getCommsHistory() { let defer = this.$q.defer(); this.localCommsHistoryDB ...

Leveraging Angular's capability to import files directly from the assets

I recently installed a library via npm and made some modifications to one of the modules. python.js If I delete the node_modules folder and run npm install, I am concerned that I will lose my changes. Is there a way to preserve these modifications by mov ...

Tips on reusing the next-auth initFirestore object to retrieve additional information from Firestore

I have successfully set up a NextJS v13.4.2 project with next-auth v4.22.1 to enable users to authenticate using the 'Login with Google' button. Additionally, I am utilizing the next-auth Firebase adapter in my current setup. The relevant configu ...

Tips for hiding a bootstrap modal in Angular4 when it has no content

I am currently working on an Angular 4 e-commerce application. One of the requirements is to hide a bootstrap modal when there are no products in the cart. When a user selects some products, they are added to the mycart modal screen. The user also has the ...

Tally up identical words without considering differences in capitalization or extra spaces

Let's take an example with different variations of the word "themselves" like "themselves", "Themselves", or " THEMSelveS " (notice the leading and trailing spaces), all should be considered as one count for themselves: 3 ...

Is there a way to successfully deploy a Google Cloud Function that relies on a tgz file as a dependency?

Due to some unspecified reasons, I am required to update a current Google Cloud Function operating on Node 10 to incorporate a local tgz file as one of its dependencies (created using npm pack; functions correctly locally). However, upon including the arch ...

What is the proper error type to use in the useRouteError() function in react-router-dom?

In my React project, I am utilizing the useRouteError() hook provided by react-router-dom to handle any errors that may arise during routing. However, I'm uncertain about the correct type for the error object returned by this hook. Currently, I have ...

Is it possible to generate a Date object from a predetermined string in typescript?

I have a string with values separated by commas, and I'm trying to create a Date object from it. It seems like this is not doable -- can someone verify this and provide a solution if possible? This code snippet doesn't work : let dateString=&a ...

In Vue, you can dynamically load a JavaScript file containing a JavaScript object during runtime

I'm in the process of developing a no-code application with Vue. I have come across an issue where I cannot add functions to a JSON file that I want to import at runtime. As a workaround, I decided to use a JavaScript or TypeScript file to store the J ...

Error: Unable to access _rawValidators property of null object

I'm currently facing an issue with formgroup and formcontrol in Angular. When I run ng serve, it's showing an error in the console. Does anyone have a solution for this? TypeError: Cannot read properties of null (reading '_rawValidators&a ...

Optimal methods for developing a photo application using React Native and Firebase Storage

In my latest project, I am utilizing React Native to develop an application similar to Instagram, with a firebase backend. However, I have encountered some challenges with the Firebase storage where saved images return an 'https' link that requir ...

Executing a function once the Angular Component's DOM has been updated, just like how ngAfterViewInit works

I have a unique feature in my component that creates multiple directives based on user input. The simplified example below demonstrates how the component receives configuration data through an Input(), processes it using a service called MyService to gener ...

Improving Performance: Addressing Charset Definition Issue in NextJS Lighthouse Best Practices

Encountering an error on my blog page that states: Properly define charset Error! A character encoding declaration is required. It can be achieved with a tag in the first 1024 bytes of the HTML or in the Content-Type HTTP response header. Find out more a ...

An error occurred while trying to set the property 'IS_CHECK' of an object that is undefined

I'm attempting to create a checkbox that, when selected, should also select everything else. I followed the code example provided in this tutorial for angular 2. However, I encountered an error: "ERROR TypeError: Cannot set property 'IS_CHECK&ap ...

The parameter failed to initialize causing the HTTP service to fire prematurely

In my project, I am utilizing Angular 10. Within the ngOnInit function, I have nested HTTP calls structured like this: ngOnInit(): void { let communetyid; this.route.data.subscribe(data => { this.route.params.subscribe(params => { ...

Ensuring Immutability of React Props in TypeScript

Someone mentioned that React Props are typically read-only, but I noticed that I was able to overwrite the props value in a component without encountering any errors. Is there a way to make props truly read-only? interface Props { readonly isText: bool ...