mosh-hamedani / organic-shop

192 stars 204 forks source link

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

Open ritwickdey opened 6 years ago

ritwickdey commented 6 years ago

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 commented 6 years ago

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.

priyesh18 commented 6 years ago

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

Pranav50 commented 5 years ago

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 commented 3 years ago

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 .