The error in creating a Firebase user is detected following the execution of the 'then' block

I created a signup page in my Vue app where users can input their information. Once the input is validated, I use firebase auth to add the user. Upon successful sign up, I want the page to automatically navigate to the login page. Here is the code for my signup page:

<template>
  <div class="signup container">
      <form @submit.prevent="signup" class="card-panel">
          <h2 class="center teal-text">Signup</h2>

          <div class="field">
              <label for="firstName">First Name:</label>
              <input type="text" name="firstName" v-model="firstName">
          </div>
          <p class="red-text center" v-if="firstNameFeedback"> {{firstNameFeedback}} </p>

          // Other form fields...

          <div class="field center">
              <button class="btn deep-teal">Signup</button>
          </div>
      </form>
  </div>
</template>

// Script section...

</style>

In the code snippet provided, I handle errors from the createUserWithEmailAndPassword() method within the catch block. However, despite this setup, the page redirects to the login page first and only then executes the error handling logic. This is evident as the error message appears in the console after the redirection. Any assistance on resolving this issue would be greatly appreciated. Thank you.

Answer №1

To ensure proper promise chaining, make sure to return the promise from the asynchronous set() method and adjust the final then() block without a callback function.

Consider the following updated code snippet:

firebase
  .auth()
  .createUserWithEmailAndPassword(this.email, this.password)
  .then((cred) => {
    return db.collection('admin').doc(cred.user.uid).set({
      admin: this.$route.params.admin,
      email: this.email,
      fname: this.firstName,
      phone: this.phone,
      surname: this.lastName,
      'user.uid': cred.user.uid,
    });
  })
  .then(() => {
    this.$router.push({ name: 'Login' });
  })
  .catch((err) => {
    console.log(err.message);
    this.rePasswordFeedback = err.message;
  });

Keep in mind that it might not be necessary to redirect to the login page since the createUserWithEmailAndPassword() method already logs in the user upon successful account creation (as per the documentation, "On successful creation of the user account, this user will also be signed in to your application"). You could consider redirecting directly to a secure page instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Communication between React.js and Node.js in VS Code

I recently started learning React and node js. I managed to create a simple "Hello World" project in reactjs, which works perfectly. App.js import React, { Component } from 'react'; import logo from './logo.svg'; import './App.cs ...

The compatibility between json_encode and JSON.parse is not seamless

I am encountering a situation on my PHP page where I am using json_encode and getting the following output: {"id":"761","user":"Moderator","message":"test"} {"id":"760","user":"Patrick","message":"test"} My goal is to parse these values using JSON.parse ...

Retrieve the value of the chosen option from a dropdown menu that adjusts dynamically within

I have a query that involves dynamically adding rows to a table for data insertion. A particular <td> element houses a dropdown menu, and I need to extract the selected value from this dropdown in order to make an AJAX call back to a PHP script that ...

Tips for managing an interval for play, pause, and stop functions in JavaScript or Node.js

In my main file, I have an API to control the playback of a video. main.js const { mainModule } = require('process'); const { startVideo, pauseVideo, stopVideo } = require('./modules/video.js'); function init(payload) { if(payl ...

Modifying an element using JQuery draggable upon commencement

Can the element being dragged be changed within the 'start' method? For example, let's say I have a button labeled 'yellow square' that is draggable. Even though it says yellow square, the actual object being dragged is a yellow sq ...

Leveraging ng-selected in AngularJS to effortlessly select multiple options from a collection

Two arrays of objects are causing me some confusion, with one array being a subset of the other: $scope.taskGroups = [ {id: 1, name: 'group1', description: 'description1'}, {id: 2, name: 'group2', description: 'descr ...

Issue with variable creation within ng-repeat

After reading a question on StackOverflow about determining the number of filtered out elements, I wrote the following code in Angular: <input type="text" ng-model="query" placeholder="Enter query..."> <div ng-if="filteredData.length!=data.length ...

Verify whether a document retrieved from mongoDB contains a certain property

Dealing with user accounts in Mongoose, I have set it up so that the user can use their phone number to sign in: const account = await db.Account.findOne({ phone: req.body.phone }) : Now, I need to confirm if there is a property named verified in the acco ...

Using TypeScript, one can easily employ a jQuery element within Vue's 'data' option

Is there a way to store a reference of a jQuery element in the Vue 'data' object without causing TypeScript errors? Any suggestions on how to resolve this issue? [EDIT] I managed to work around the TypeScript error by setting a non-existing jQu ...

Just starting out with React and encountering the error: Invalid element type, a string was expected

I seem to be going in circles with the following issue as I try to load the basics of a React app into the browser. An error message stating 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite c ...

Having trouble disabling an HTML select menu using JQuery?

My HTML page includes multiple select menus that all have the shared class name .iconDropDownMenu. During generation with PHP, some of these select menus may be hidden by PHP adding an additional class. I attempted to disable only the hidden .iconDropDown ...

What is the best way to adjust the width of a textarea based on its content

How can I dynamically set the width of a React Semantic UI textarea based on its content? Setting min-width doesn't seem to be working. Any suggestions? <Textarea key={idx} defaultValue={formattedText} className="customInpu ...

Retrieving a numerical value from a string using Javascript or JQuery

Here is an example string: "example example-5 amount-10 example direction-left" Is there a way to extract the number following "amount-", and the text immediately after "direction-" in this string? ...

Error encountered when building with --prod flag due to AngularFireFunctions injection issue

After successfully using AngularFireFunctions in a modal during development, I encountered an error after compiling with the --prod flag. The issue seems to be related to the '"optimization": true' setting in the angular.json file and its intera ...

Navigating asynchronous URL parsing in NodeJS

I am still fairly new to the world of Node.js and Javascript, so I appreciate your patience with my confusion around the callback mechanism Bacchanalia. The Issue at Hand I'm currently working on a basic Node.js application that is responsible for h ...

Using VueJs to dynamically adjust the width of an element based on another element

I’m currently developing a Trivia app that features an <h1> element which dynamically changes every 10 seconds. My goal is to align the width of a radio button group component (b-form-group from Bootstrap-Vue components) with the width of the <h ...

Issue with React Native and Redux: Prop values are updating without being called

I'm currently facing a problem with React Native and Redux integration. Using a Redux state to toggle a modal visibility between components seems like the most efficient solution due to its cross-component nature. The modal opens and closes as expec ...

What is the best way to implement a nested side menu in Ionic Framework?

Greetings! I'm currently attempting to incorporate a nested menu within a side menu. For instance, take a look at this example: http://codepen.io/ionic/pen/QwamEW When I select a menu item, a new page (new form) opens. However, I would like for th ...

Spring application: Unable to find a handler for portlet request with mode 'view' and phase 'Resource_PHASE'

It seems like everything is set up correctly, but for some reason, my ajax call fails with the error message "No handler found for portlet request: mode 'view', phase 'Resource_PHASE'". The handler URL I'm using is "getAllFruit", ...

Breaking the condition within the while loop (Now featuring a code snippet)

Let's clarify the condition in the while loop, which should check if the new challenge number has not been completed yet (as indicated in the challenges.finished array). This means that the same challenge can be displayed multiple times instead of jus ...