title: Firebase Cloud Functions called multiple times
body: <p>Hello my firebase cloud function gets called multiple times when I don't put in check for previous.exists().
  I get multiple push notifications.</p>
  
  <pre><code>if (!event.data.exists()){
      return;
  }
  if (event.data.previous.exists()){
      return;
  }
  </code></pre>
  
  <p>But when I check for it i don't get push notification.
  Here is the not working code:
  What should I change?</p>
  
  <pre><code>exports.sendShoppingListInvitationNotification = functions.database.ref('/invites/{id}/').onWrite(event =&gt; {
  //get the snapshot of the written data
  const snapshot = event.data;  
  
  if (!event.data.exists()){
      return;
  }
  if (event.data.previous.exists()){
      return;
  }
  
      //get snapshot values
      console.log(snapshot.key);
  const receiptToken = snapshot.child('receiptFcmToken').val();
  const senderName = snapshot.child('senderNickname').val();
  const inviteMessage = snapshot.child('inviteMessage').val();
  const senderImage = snapshot.child('senderProfileImageURL').val();
  
  
  //create Notification
  const payload = {
      notification: {
          title: `Invitation from ${senderName}`,
          body:  `${inviteMessage}`,
          icon: `${senderImage}`,
          badge: '1',
          sound: 'default',
      }
  };               
  
  //send a notification to firends token   
  return admin.messaging().sendToDevice(receiptToken, payload).then(response =&gt; { 
       console.log("Successfully sent message:", response);
   }).catch((err) =&gt; {
      console.log(err);
  });   
  });
  </code></pre>
  
  <p>I don't get error message on cloud console.</p>
  
  <p>This is the firebase structure:
  <a href="https://i.stack.imgur.com/bEKZp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bEKZp.png" alt="enter image description here"></a></p>