1

I make a Project which by firebase and Angular.

First this is my ts code.

...
async ngOnInit() {
        this.email = await this.loginServise.userInfo();
        this.arrayBouquetName = await this.fireStore.collection("user").doc(this.email).get().toPromise();
        this.arrayBouquetName = await this.arrayBouquetName.get("src");
        this.arrayBouquetSrc = await this.getSrc();
        console.log(this.arrayBouquetSrc);
    }


    async getSrc() {
        return new Promise(async(resolve, reject) => {
            for await (let item of this.arrayBouquetName){
                this.arrayBouquetSrc.push(item);
            }
            console.log("Does it work? : ",this.arrayBouquetSrc)
            resolve(this.arrayBouquetSrc);
        })
    }
...

and this is my template file (HTML)

<div id="canvas-my-bouquet">
    <div id="container-my-bouquet">
        check it : {{arrayBouquetSrc}}
    </div>
</div>

When i run this code i can't see value of arrayBouquetSrc below picture
enter image description here
however,When i change arrayBouquetSrc => arrayBouquetName, I can see value of variable.
enter image description here
I don't know what is reason and how to solve this situation...
I know If i dynamic

1
  • Why are you modifying arrayBouquetSrc in getSrc(), and then returning the same array in order to assign it to the same variable again? Commented Sep 18, 2021 at 6:56

2 Answers 2

2

Try using the async pipe as recommended by angular:

private userInfo$: Observable<User>;
private bouquets$: Observable<Boucket[]>;
private boucketSources$: Observable<string[]>;

constructor(private fireStore: FireStore) {
    // Don't use toPromise inside this method
    this.userInfo$ = this.loginServise.userInfo();
    
    // When the userInfo$ changes, map it to get the documents from FireStore
    this.bouquets$ = this.userInfo$.pipe(map((userInfo) => {
        return this.fireStore.collection("user").doc(userInfo).get();
    }));

    // Then you want the names to be in a seperate variable
    this.boucketSources$ = this.bouquets$.pipe(map((bouckets) => {
        return bouckets.map(b => b.src);
    }));
}

Now you can use the async pipe in your template

<div id="canvas-my-bouquet">
    <div id="container-my-bouquet">
        <div *ngFor="let boucket of (boucketSources$ | async)" class="boucket">
            check it : {{boucket}}
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this: replace your method.

...
async ngOnInit() {
        this.email = await this.loginServise.userInfo();
        this.arrayBouquetName = await this.fireStore.collection("user").doc(this.email).get().toPromise();
        this.arrayBouquetName = await this.arrayBouquetName.get("src"); // does this line work?
        this.fillArrayBouquetSrc();
        console.log(this.arrayBouquetSrc);
    }


    fillArrayBouquetSrc() {
         for (let item of this.arrayBouquetName){
           this.arrayBouquetSrc.push(item);
         }
         console.log("Does it work? : ",this.arrayBouquetSrc)
    }
...

NOTE: You dont need to make the method async. There is a synchronous operation. And do not return anythink from the mothod. Bcz of that it is already changing this.arrayBouquetSrc. It is available as this.arrayBouquetSrc in the class instance methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.