organic-shop icon indicating copy to clipboard operation
organic-shop copied to clipboard

Total number of cart item at navbar is not updating for only first time

Open ritwickdey opened this issue 6 years ago • 4 comments

Hi Mosh. First, - a lot of thanks for creating such awesome course with a shopping site project.

I found a issue that the total no of cart is not updating for only first time of visit (I mean, if the value of cartId is null/ undefined at LOCAL STORAGE).

image

If I reload the page, that is working perfectly fine.

ritwickdey avatar Sep 04 '17 13:09 ritwickdey

I've noticed, this issue is occurring due to two async call on getOrCreateCartId(). 2nd async is called before the 1st async task is completed (call to firebase + store the key value to localstoage). That's why there are 2 call to server & 2 unique key generated.

https://github.com/mosh-hamedani/organic-shop/blob/403b20f74b9156f42d0a5d823e0cd765dea41af3/src/app/shared/services/shopping-cart.service.ts#L44-L51

if i write the code like this (temporary solution)

private async getOrCreateCartId(): Promise<string> {  
   let cartId = localStorage.getItem('cartId'); 
   if (cartId) return cartId;  
  
   let result = await this.create(); 
   /*checking again, if value if already set by 1st async process.*/
   let cartId = localStorage.getItem('cartId'); 
   if (cartId) return cartId; 

   localStorage.setItem('cartId', result.key); 
   return result.key; 
 } 

I know, this is bad code, I am repeating 2 same line.... however, still we're still hitting server 2 times for getting key.

Please let me know if I'm wrong.

ritwickdey avatar Sep 04 '17 15:09 ritwickdey

@ritwickdey Can you explain which are the 2 async calls to getOrCreateCartId() in detail.

priyesh18 avatar Nov 15 '17 06:11 priyesh18

If anyone wants this project in Angular 7, watch out my Github https://github.com/Pranav50/E-Commerce Do not forget to rate star.Thanks...

Pranav50 avatar May 17 '19 03:05 Pranav50

Ok thanks

On Wed, May 26, 2021, 6:33 AM faysalwzbm @.***> wrote:

Just simply remove the await from the 4th line.

let result = this.create();

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/organic-shop/issues/1#issuecomment-848380787, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEKIMZ4CA4MX7F7XNTCYGZTTPRCHZANCNFSM4DZPZGPA .

Pranav50 avatar May 26 '21 05:05 Pranav50