Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
¿Mi promesa devuelve el mismo valor cada vez que se llama a pesar de tener datos diferentes?

Entonces, primero se llama a la función getCart en b-navbar.component.ts :

 export class BNavbarComponent implements OnInit{ appUser: any; cart$ : Observable<ShoppingCart | null>; constructor(private auth : AuthService, private shoppingCartService : ShoppingCartService) {} async ngOnInit() { this.auth.appUser$.then(dataObservable => { dataObservable?.subscribe(data => { this.appUser = data }); }); this.getCart() } async getCart() { this.cart$ = await this.shoppingCartService.getCart() this.cart$.subscribe(data => { console.log("Nav Observable data: ", data); }) }

Que busca en shopping-cart.service.ts y obtiene la promesa de un observable:

 export class ShoppingCartService { constructor(private db : AngularFireDatabase) { } private create(){ return this.db.list("/shopping-carts").push({ // dateCreated : new Date().getTime() date: "date" }); } async getCart(): Promise<Observable<ShoppingCart>>{ let cartId = await this.getOrCreateCartId(); let cart = this.db.object("/shopping-carts/" + cartId); return new Promise((resolve) => { cart.valueChanges().subscribe(x => { let newX = x as ShoppingCart let items = newX.items resolve(of(new ShoppingCart(items))) }) }) } private async getOrCreateCartId() : Promise<string> { let cartId = localStorage.getItem("cartId"); if (cartId) return cartId; let result = await this.create(); localStorage.setItem("cartId", result.key as string); return result.key as string; } }

Ahora surge el problema al interpolar los valores en mi html, ya que el observable devuelto en la promesa getCart resuelve un observable "estático", lo que significa que termina el observable cuando se resuelve, por lo que los datos nunca se actualizan. Por favor ayuda :))

 <a class="navbar-item" routerLink="/shopping-cart"> Shopping Cart <span *ngIf = "cart$ | async as cart" class="tag is-warning is-rounded" > {{ cart.totalItemsCount }} </span> </a>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Una vez que su promesa se resuelve, no "emite" nada después de eso . Esta es una diferencia clave entre promesas y observables . Los observables pueden emitir más valores incluso después de emitir el primero.

Por lo tanto, debe hacer que su getCart devuelva solo la secuencia observable:

 import { map } from 'rxjs'; // rest of your code async getCart(): Promise<Observable<ShoppingCart>>{ let cartId = await this.getOrCreateCartId(); let cart = this.db.object("/shopping-carts/" + cartId); return cart.valueChanges() .pipe(map(x => { let newX = x as ShoppingCart let items = newX.items return new ShoppingCart(items) })) }

De esta manera, su promesa se resuelve en el flujo observable en lugar de en un solo valor.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!