繁体   English   中英

触发云 function 后 Firestore DB 未更新?

[英]Firestore DB not getting updated after firing cloud function?

我有一个云 function,它会在某个文档更新时更新。 function 被正确触发。

当我到达 function 的末尾时,该向文档添加数据时,什么也没有发生。

我就是这样做的,我只是看不出有什么问题。

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

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

我也想知道这是否与我的进口商品有关,但我认为它们也是有序的......

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

admin.initializeApp();

const db = admin.firestore();

编辑:根据要求,这是云的完整代码 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,
        });
    });

您的云 Function 是后台触发的,您调用了多个异步方法(来自 axios 和来自 Firebase),因此您需要正确链接这些方法返回的承诺并返回 promise 链。 Firebase 文档中有更多详细信息。

以下更改应该可以解决问题:

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,
                });

            });

    });

请注意,我们使用Promise.all()因为您需要并行执行所有 axios 调用。

另外,请注意,我让您在第二个forEach循环中调整代码: resultArray是一个数组,其中包含传递给Promise.all()的所有承诺的结果,如文档中所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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