Firebase Cloud Function仅与console.log()一起使用 [英] Firebase Cloud Function only works with console.log()

查看:64
本文介绍了Firebase Cloud Function仅与console.log()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经为我的项目编写了一些云功能.我正在构建一个新函数addUserToEmailLists并对其进行测试,以查看是否通过在创建该文档时在文档中设置一个字段来调用它,onCreate()

I've written a few cloud functions for my project so far. I was building a new function addUserToEmailLists and testing it to see if it got called simply by setting a field in a document when that document was created, onCreate()

exports.addUserToEmailLists = functions.firestore.document('Users/{userID}/jobPreferences/myPreferences').onCreate(() => {
  console.log('Function called!'); //remove this and it doesn't work
  return db.doc(`DatabaseInfo/accounts`).set({ //update the number of preferences
        preferencesSet: 1
  }).catch(error=>{
        console.log(error);
        return error;
  });
})

起初,我刚好在函数中以return开头的那一行,而Firebase日志一直在说该函数以状态200执行,但未设置文档.作为最后的手段,我添加了console.log('Function called!');并且它起作用了!然后,我删除了该行并再次进行了部署,但它未能设置字段accountsCreated(是为了确定再次尝试再次触发该功能,所以还要等待至少3分钟再进行两次).我的其他函数没有任何console.log()语句,所以我对导致这种行为的原因不知所措.有人有建议吗?

At first I had just from the line with return straight down in the function and the Firebase log kept saying the function was executed with status 200 but the document wasn't being set. Just as a last resort I added the console.log('Function called!'); and it worked! Then I removed the line and deployed once more and it failed to set the field accountsCreated (did that a couple more times waiting at least 3 minutes before trying to trigger the function again to be sure). My other functions don't have any console.log() statements so I'm at a loss for what causes this behaviour. Does anyone have a suggestion?

推荐答案

在我看来,您正在调用异步函数并期望其同步运行.

Looks to me like you are calling an asynchronous function and expecting it to behave synchronously.

也许添加console.log条目只是为该功能提供了完成异步工作所需的空间.

Perhaps the addition of a console.log entry simply gives the function the headroom it needs to complete the asynchronous work.

尝试这样的事情:

exports.addUserToEmailLists = functions.firestore.document('Users/{userID}/jobPreferences/myPreferences').onCreate(async () => {
  await db.doc(`DatabaseInfo/accounts`).set({ //update the number of preferences
        preferencesSet: 1
  });
})

这篇关于Firebase Cloud Function仅与console.log()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆