The promise ended up on a different path

I have a function implemented to update my user on firebase, but for some reason the "then" function is never called. Any ideas on how to resolve this issue?

The user's data was successfully updated in the database, and when I try to directly implement the "then" branch within the function, it works as expected.

EDIT () :


     // Continue with update
     this.statoUtente.updateCurrentUser(uteIn).then(() => {

          <<<<<<<<<<<<<<<<<<<<< program never call this >>>>>>>>>>>>>>>>>

          console.log('Response from Promise') ;  
          // After updating, prompt for completing missing information
          this.nomeTemplate = 'updateUserForm' ;
          this.helperset = 2 ;
          this.wait = false ;
          this.error = '' ;
      })




updateCurrentUser(UteIn: TipoSingoloUtente): Promise<any> {

  let Promessa = new Promise<any>(observer => {
    let userID = this.afAuth.auth.currentUser.uid ;
    console.log(userID) ;
    if ((userID) && ( userID.trim() != '' )) {
      UteIn.ID = userID ;
      observer(this.db.object('users/' + userID).set(UteIn))
    } else {
      console.log('null null null') ;
      Promise.resolve(false) ;
    }
  }) ;

  return Promessa ;

}

Answer №1

It is sufficient to have

updateCurrentUser(UteIn: UserType) {
  const userId = this.afAuth.auth.currentUser.uid;
  if (userId && userId.trim() != '') {
    UteIn.ID = userId;
    return this.db.object('users/' + userId).set(UteIn);
  } else {
    console.log('null null null');
    return Promise.resolve(false);
  }
}

No need for enclosing it in a Promise constructor. The function this.db.object... already returns a Promise, and Promise.resolve(...) also returns a Promise.

Answer №2

It is advisable to eliminate the custom promise creation as it is unnecessary. You can simply use the promise returned by the following code:

this.db.object('users/' + userID).set(userData)

Once this promise is resolved, you can be certain that the database has been updated successfully.

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

Leveraging Firebase to pull information from various nodes using a shared identifier

I am currently developing a social networking application, utilizing Firebase as my backend for APIs. However, I am facing difficulties when it comes to running queries to retrieve all the necessary data. The structure of my database is organized as follow ...

What is the process for converting variadic parameters into a different format for the return value?

I am currently developing a combinations function that generates a cartesian product of input lists. A unique feature I want to include is the ability to support enums as potential lists, with the possibility of expanding to support actual Sets in the futu ...

Nested Angular Router Resolvers

I need help with a routing issue: const routes: Routes = [ { path: ':id', component: MoyComponent, resolve: { first: MyFirstResolver second: mySecondResolver(first) } } Here is the ...

What is causing the binding error with the 'checked' attribute in Angular's Mat-checkbox component?

As a newcomer to the Angular environment, I encountered an issue with the mat-checkbox element. When attempting to bind a boolean to its checked property, I received the error message: "Can't bind to 'checked' since it isn't a known pro ...

Access Denied: Origin not allowed

Server Error: Access to XMLHttpRequest at '' from origin 'http://localhost:4200' has been blocked by CORS policy. The 'Access-Control-Allow-Origin' header is missing on the requested resource. import { Injectable } from &apo ...

Include a .done() callback within a customized function

In the small class, I have included these functions: var Ajax = { // Send new entry data to database endNewEntry: function (json) { $.post("/controllers/insertEntry.ajax.php", {"json": json}); }, loadView: function (view, target, extra) { ...

What steps should be taken to rectify the issue of a missing type in a property within an

After spending the last 2 years working on Angular, I am now encountering an issue with a new interface for the first time. The specific error message is as follows: Type 'MatTableDataSource<Tasks[]>' is missing the following properties f ...

What is the correct way to submit a formarray in an angular application following the specified format?

When submitting a form in Angular, I'm facing an issue where only the data from the first index inside the role menu is being passed. How can I ensure that all index data is passed on submit? { "roleMenu":[{ "menu":{ "menuId": 1 }, ...

Exploring the power of IdentityServer4 in conjunction with an ASP.NET core API and Angular for seamless

I have implemented IdentityServer4 for authentication and authorization in my ASP.NET core API, with Angular4 on the client side. I am currently using the token endpoint (http://myapidomain/connect/token) to obtain an access_token using grant type = Resour ...

the upward arrow remains steadfast, refusing to evolve into its downward counterpart

Every time I click on "Portfolio", the arrow points downwards. https://i.sstatic.net/Gwf5F.png However, when a submenu appears, the arrow changes to point upwards. https://i.sstatic.net/kFdjr.png If I click on "Portfolio" again, the arrow remains point ...

Is it permissible to incorporate a snackbar within my service?

I am trying to retrieve error details from a service call that communicates with an API in case of an error occurrence. For example, if the HTTP error returned is 422, I want to display a user-friendly message. Below is my service class: @Injectable() e ...

When fetching data from the API in Angular, the response is displayed as an object

After fetching data from the API, I am encountering an issue where the "jobTitle" value is not displaying in the table, but instead appears as an array in the console. Can someone assist me with resolving this problem? Below is the visibility.ts file: exp ...

Guide on displaying console.log in mobile simulator while using Angular2

Interested in debugging JavaScript on mobile devices, particularly Android and iPhone. Seeking a code snippet or library that can display console.log messages on the screen of a phone emulator. Ideally looking for a solution that works with angular2 and i ...

In order to emphasize the chosen list item following a component refresh

SCENARIO: Let's consider a scenario where I have a component named list, responsible for displaying a list of all customers. Within this list, certain conditions are set up as follows: 1) Initially, the 1st list-item (e.g. Customer 1) is selected by ...

A guide on utilizing Material UI Fade for smoothly fading in a component when a text field is selected

I am facing an issue with a text field input and a helper component. My goal is to have the helper component fade in when a user focuses on the input field. The helper component is wrapped as follows: <Fade in={checked}> <DynamicHelperText lev ...

Issue with resolving symbol JSON in Angular 7 when using JSON.stringify

Let me start off by saying that I am new to Angular 7. Currently, I am in the process of developing an application using Angular 7 with a C# backend. The specific challenge I am facing is the need to serialize an object in my component/service before sendi ...

What is the proper method to transmit Firebase authentication tokens to a backend server?

I am looking to securely identify the currently signed-in user on my nodejs server. After a successful sign-in, I need to send the user's ID token to the server using HTTPS for verification. Following the guidance in Firebase documentation: firebase ...

Mapping an array of JSON objects into a plain JSON object in TypeScript Angular 2: A Guide

Response Data: [ { "professionalId": { "cib_code": "30003", "thirdPartyId": "RS30004" }, "nationalIdentifier": "984538926", "nationalIdType": "SIREN" }, { "professionalId": { "cib_code": "30003", "thirdPar ...

Modal component not displaying properly in ng-bootstrap framework

I'm having trouble getting a modal and its background to display when activated. The elements are being created and destroyed correctly in the developer tools, but they are not visible on the page. I'm trying to replicate the example from this li ...

Is there a solution available for the error message that reads: "TypeError: Cannot set value to a read-only property 'map' of object '#<QueryCursor>'"?

Everything was running smoothly in my local environment, but once I deployed it on a Digital Ocean Kubernetes server, an error popped up. Any assistance would be greatly appreciated. https://i.stack.imgur.com/VxIXr.png ...