简体   繁体   中英

Mailgun in Firebase Cloud Function send several times by triggered once

Try to run the function in Firebase Cloud Function once Cloud Function data generates. Actually, I want to run sendEmail triggered by adding the events collection's data. But the events occur several times not once.

I use mailgun for sending an email.

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

If you want to perform a Firstore query a single time, don't use onSnapshot . That sets up a listener on a document that gets triggered whenever the document changes. You almost certainly want to use get() instead, which performs a query a single time.

Also, you are not returning a promise that resolves when all the asynchronous work is complete. That is required for all Cloud Functions that are not HTTP functions. The promise is how Cloud Functions knows when it's safe to terminate and clean up all work, as described in the documentation . get() returns a promise, so you should use that in addition to other promises for async work that you start. If you don't handle promises correctly in your function, it will not behave the way you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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