Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase云函数中的Mailgun一次触发多次发送_Javascript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript Firebase云函数中的Mailgun一次触发多次发送

Javascript Firebase云函数中的Mailgun一次触发多次发送,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,一旦云函数数据生成,尝试在Firebase云函数中运行该函数。实际上,我想通过添加事件集合的数据来运行sendmail但事件会发生多次,而不是一次。 我使用mailgun发送电子邮件 exports.sendEmail = functions.firestore .document("events/{eventId}") .onCreate((snap, context) => { const data = snap.data(); const { uid } =

一旦云函数数据生成,尝试在Firebase云函数中运行该函数。实际上,我想通过添加
事件
集合的数据来运行
sendmail
但事件会发生多次,而不是一次。

我使用
mailgun
发送电子邮件

exports.sendEmail = functions.firestore
  .document("events/{eventId}")
  .onCreate((snap, context) => {
    const data = snap.data();
    const { uid } = data;
    usersRef.doc(uid).onSnapshot((user) => {
        firestoreRef
          .collection("followers")
          .where("uid", "==", uid)
          .get()
          .then((snapshot) => {
            snapshot.docs.map((snapshot) => {
              const follower = snapshot.data();
              mailgunClient.messages
                .create("mg.xxxx.com", {
                  from: "Excited User <noreply@mg.xxxx.com>",
                  to: follower.email,
                  subject: Hello,
                  text: "test",
                  html: "<p>test</p>",
                })
                .then((msg) => console.log("msg", msg))
                .catch((err) => console.log("error", err));
            });
          });
      });       
    }
    return true;
  }
exports.sendmail=functions.firestore
.document(“events/{eventId}”)
.onCreate((快照,上下文)=>{
const data=snap.data();
常量{uid}=数据;
usersRef.doc(uid).onSnapshot((用户)=>{
firestoreRef
.收藏(“追随者”)
。其中(“uid”,“uid=”,uid)
.get()
。然后((快照)=>{
snapshot.docs.map((快照)=>{
const follower=snapshot.data();
mailgunClient.messages
.create(“mg.xxxx.com”{
发件人:“兴奋用户”,
致:follower.email,
主题:你好,
文本:“测试”,
html:“测试

”, }) .然后((msg)=>console.log(“msg”,msg)) .catch((err)=>console.log(“error”,err)); }); }); }); } 返回true; }
如果您想一次性执行Firstore查询,请不要使用
onSnapshot
。它会在文档更改时触发的文档上设置侦听器。您几乎可以肯定要使用
get()
,它只执行一次查询

此外,您不会返回在所有异步工作完成时解析的承诺。这是所有非HTTP函数的云函数所必需的。承诺是指云函数如何知道何时可以安全终止和清理所有工作,如。
get()中所述
返回一个承诺,因此除了开始异步工作时的其他承诺外,还应使用该承诺。如果在函数中未正确处理承诺,则函数的行为将与预期不符