title: Best way to use observable once
body: <p>The code below does the following:</p>
  
  <p>The upload function below is being called on a file input when it changed. As you can see it creates a message with the uploaded image.</p>
  
  <p>I get the user data from the user observable in the authService class (this user observable is received by firestore).</p>
  
  <p>Once the message is created, a server-side trigger will update the number of messages the user has send (increment counter). This results in the user object being updated in the database. <strong>Since this is an observable, the logic starts again and the code keeps upload images (infinite loop).</strong></p>
  
  <p><strong>Is there a better way to get the userId (without observable), or to only execute the function once? What's the best, cleanest and most valid approach here?</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>    upload(fileEvent) {
  
          const message = new Message();
          message.createdAt = new Date();
          message.fileLocation = 'loc';
          message.receivedAt = null;
          message.receivedMessageId = null;
          message.receivedUserId = null;
  
          this.authService.user.subscribe(user =&gt; {
            message.sendUserId = user.userId;
            message.sendScreenName = user.screenName;
  
            const messageImage = fileEvent.target.files[0];
  
            this.messageService.createMessage(message, messageImage);
  
            user.un
          });
        }
  
  </code></pre>