【问题标题】:Firestore cloud function giving log issuesFirestore 云功能给出日志问题
【发布时间】:2019-11-12 16:52:00
【问题描述】:

我正在运行 Firestore 云功能并且输出是预期的,但在日志中我看到一个错误

函数返回未定义的、预期的 Promise 或值

我不知道为什么在我退回批次时它会这样说。我将如何删除此日志错误,以及访问文档 TYPES 以便我不能使用 ANY 类型。

export const subscriptionAdded = functions
    .firestore
    .document(`/User/{userId}/following/{subscriptionId}`)
    .onCreate((change: any, context: functions.EventContext) => {
        admin.firestore()
            .collection(`/Challenge`)
            .where('user_id', '==', context.params.subscriptionId).get().then((snapshot: any) => {
                const batch = admin.firestore().batch();
                snapshot.forEach((doc: any) => {
                    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
                    let autoId = ''
                    for (let i = 0; i < 20; i++) {
                        autoId += chars.charAt(Math.floor(Math.random() * chars.length))
                    }
                    const userChallenges = admin.firestore().doc(`/Subscribed_Challenges/${context.params.userId}/myChallenges/${autoId}`)

                    batch.set(userChallenges, {
                        challegeId: context.params.subscriptionId,
                        subscriptionUserId: context.params.userId,
                        dateTime: new Date()
                    })
                });
                return batch.commit().catch((err: any) => {
                        console.log('Batch Error', err)
                    });
            }).catch(err => {
                console.log('Error getting documents', err);
            });
    })

【问题讨论】:

    标签: javascript google-cloud-firestore google-cloud-functions


    【解决方案1】:

    问题在于你对承诺的处理。

    确实,您正在退回批次,但它来自承诺链的深处。

    在顶层,您没有返回承诺(或其他任何内容)。虽然它是一个简单的修复,但您只需要这样做:

            return admin.firestore()
                .collection(`/Challenge`)
                // ...... and the rest of your code goes here as you would expect
    

    此外,在为onCreate 触发器创建引用时没有真正的理由使用模板文字(反引号字符串)——它很容易导致错误或混乱(尽管我怀疑它在这里有效)。普通的单引号就可以了。


    要回答您关于类型的问题,最好只引用SDK Documentation,但是您从查询中调用get() 会返回一个解析为QuerySnapshot 的promise,并且传递了forEach() 回调QueryDocumentSnapshot。如果您指的是文档本身(例如 QueryDocumentSnapshot.data() 调用的结果),那将是 DocumentData

    它们都在firestore namespace admin 模块中可用,例如:

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

    会让您以admin.firestore.QuerySnapshot 的身份访问QuerySnapshot


    我还注意到您从未真正从forEach 中的任何文档中读取任何内容。我不确定这是不是故意的。

    【讨论】:

    • 这还不够。调用batch.set() 也会忽略许多承诺。
    • @DougStevenson 除非我误解了,因为这是使用批量写入,batch.set()doesn't return a promise,所以只有最终提交。虽然它返回的结果可以用于链接.set() 调用,但在这里也可以安全地忽略它。
    • 谢谢大家。我只读了id。但我找不到这些类型的接口。我在源代码中看不到它
    • 我已经添加了获取类型的确切方法。不过,请记住,如果您使用的是打字稿而不是 javascript(问题标签说的是 javascript,但我意识到它们是稍后编辑添加的),您需要 jump through hoops 才能使其工作,而且它不是超级流畅.
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2018-05-09
    • 2019-12-12
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多