firebase 由于未定义的“uid”,计划的云函数未返回值

6rqinv9w  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(143)

我写这篇文章是因为我面临着一些问题,我预定的云功能。

**第一步:**访问/ collection:users => doc:uid => collection:bank => doc:account。
**第二步:**每24小时将我的用户(所有用户)的dailyRewardCounter增加150。

问题是我的函数不能访问我的用户的集合uid并返回错误,因为预定函数不能读取uid(参见图片)。

问题: 你知道如何根据每个用户的个人“uid”访问每个用户的子集合,以便在集合“bank”中添加150吗?

enter image description here

  1. export const dailyCreditReinitialize = functions.pubsub.schedule('0 0 * * *').onRun(async (context) => {
  2. const uid = context.auth!.uid; // seems like the function does not read that
  3. const userRef = db.collection('users').doc(uid);
  4. const userSnap = await userRef.get();
  5. const getUid = userSnap.data()!.uid;
  6. const bankCollectionRef = db.collection('users').doc(getUid).collection('bank');
  7. return bankCollectionRef.get()
  8. .then(querySnapshot =>{
  9. if (querySnapshot.empty){
  10. console.log("Nothing to return")
  11. return null;
  12. } else {
  13. let batch = db.batch();
  14. querySnapshot.forEach(doc => {
  15. batch.update(doc.ref, {
  16. dailyRewardCounter: 150,
  17. });
  18. });
  19. return batch.commit();
  20. }
  21. })
  22. .catch(error => {
  23. console.log(error);
  24. return null;
  25. });
  26. })

字符串

cuxqih21

cuxqih211#

与实时数据库触发的云函数相反,对于计划的云函数,context.auth为null。
如果我正确地理解了你的问题(“增加我的用户(所有的)”),你需要循环所有的用户文档的集合。一些沿着以下行的伪代码:
1.在users集合上循环并获取所有用户ID
1.对于每个用户,获取bank集合的querySnapshot
1.在每个querySnapshots上循环更新银行单据

相关问题