简体   繁体   中英

Firestore DB not getting updated after firing cloud function?

I have a cloud function that gets updated when a certain document gets updated. The function is being triggered correctly.

It's when I reach the end of the function, when it's time to add data to the document, that nothing happens.

This is how I'm doing it, and I just don't see what's wrong.

    const docRef = db.collection('collection_name').doc('doc_name');

    let setDocRef = docRef.set({
        variable_name: 'something',
    });

I was also wondering if it's something with my imports but I think they're in order as well...

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

admin.initializeApp();

const db = admin.firestore();

EDIT : As requested, heres the complete code of the cloud function

exports.myFunctionName = functions.firestore
    .document('docName').onUpdate((change, context) => {
        const usernames = []; //a list of usernames which im not filling here
        let topValueIds = [];
        // retrive username from settings here
        axios.get('an-endpoint').then(resp => {

            // Get top Values
            const topValues = []; //some filtered array

        let topPicks = [];
        
        usernames.forEach(usr => {
            axios.get('endpoint').then(resp => {
                let score = 0;

                // Get top Values for person being queried
                const matchValues; // a sliced array
                matchValues.forEach(value => {
                    if(topValueIds.includes(value.id)) {
                        score++;
                    }
                });
                if (score >= 1) {
                    topPicks.push(resp.data.person.publicId)
                }
            });
        })

        const docRef = db.collection('collection-name').doc('doc-name');

        let setTopPicks = docRef.set({
            user_list: topPicks,
        });
    });

Your Cloud Function is a background triggered one and you call several asynchronous methods (from axios and from Firebase), therefore you need to correctly chain the promises returned by these methods and return the promise chain. More details in the Firebase doc .

The following changes should do the trick:

exports.myFunctionName = functions.firestore
    .document('docName').onUpdate((change, context) => {
        const usernames = []; //a list of usernames which I'm not filling here
        let topValueIds = [];

        return axios.get('an-endpoint')
            .then(resp => {

                // I guess you use the value of resp somewhere in this then() block
                // But it is not clear where, from your question...

                const topValues = []; //some filtered array
                let topPicks = [];

                const promises = [];

                usernames.forEach(usr => {
                    // No use of the usr value??? You probably use it to define the URL called by axios...
                    promises.push(axios.get('endpoint'));
                });

                return Promises.all(promises);
            })
            .then(resultsArray => {

                resultsArray.forEach(result => {
                    // .... Up to you to adapt this part!
                    // resultArray is an Array containing the results of all the promises passed to Promise.all(), i.e. the results of the axios calls
                });

                const docRef = db.collection('collection-name').doc('doc-name');
                return docRef.set({
                    user_list: topPicks,
                });

            });

    });

Note that we use Promise.all() because you need to execute all the axios calls in parallel.

Also, note that I let you adapt the code within the second forEach loop: resultArray is an Array containing the result of all the promises passed to Promise.all() , as explained in the doc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM