Angular - доступ к хранилищу ngrx в блоке then - PullRequest
0 голосов
/ 21 декабря 2018

Я использую Angular6 и Cloud Firestore (не база данных в реальном времени).У меня есть служба, которая содержит функцию, которая добавляет документ в коллекцию, а затем использует блок .then, чтобы получить идентификатор только что созданного документа, а затем выполнить другую логику.В этой функции я хочу получить доступ к некоторому содержимому корзины покупок, которое я храню в Store, используя ngrx.В моем ngOnInit у меня есть следующий код, который подписывается и получает содержимое и присваивает его cartContents (глобальная переменная в службе).

this.myState = this.store.select('shoppingCart');
this.shoppingCartStateSubscription = this.myState.subscribe(val => {
   this.cartContents = val.basketList;
});

Затем я хочу получить возможность доступа к значению this.cartContents внутри блока .then, однако значение отображается как неопределенное.Что я делаю неправильно / недоразумение?

Код для функции:

processPayment(token: any, amount: number){
        const payment = { token, amount };

this.paymentCollection.doc(`${this.userId}`).collection('payments').add({
            payment
        })
        .then(docRef => {
            console.log("Document written with ID: ", docRef.id);
            this.docRefId = docRef.id;
            this.db.collection("payments/"+`${this.userId}`+"/payments").doc(`${this.docRefId}`)
        .onSnapshot(function(doc) {
            console.log("Current data: ", doc.data());
            if(doc.data().status === "succeeded"){

                const updatedBalance = this.balance - amount;;

                let writeBatch = firebase.firestore().batch();

                //Update purchased lessons ref
                console.log(this.cartContents); //APEARS AS UNDEFINED

                for(let buyable of this.cartContents){
                    let purchasedLessonsRef = this.afs.firestore.doc(`/purchased_lessons/${this.userId}/lessons/${buyable.b_id}`);
                    writeBatch.update(purchasedLessonsRef, buyable);
                }

                //Update balance ref
                let userBalanceRef = this.afs.firestore.doc(`/users/${this.userId}`);
                writeBatch.update(userBalanceRef, {balance: updatedBalance});

                this.docRefId = null;

                return writeBatch.commit().then(function () {
                    console.log("commiting batch")
                });
            }
        });

        console.log("finished function");
        })
    .catch(error => console.error("Error adding document: ", error))
}

1 Ответ

0 голосов
/ 21 декабря 2018

Если вы получаете значение для OnNgInit(), единственная причина, о которой я могу подумать, - это привязка this.

Подозреваемый - function(doc){//stuff}, измените его на (doc) => {//stuff}

Вы также можете попробовать заменить все ваши функции на arrow.

    processPayment = (token: any, amount: number) =>{
        const payment = { token, amount };

this.paymentCollection.doc(`${this.userId}`).collection('payments').add({
            payment
        })
        .then(docRef => {
            console.log("Document written with ID: ", docRef.id);
            this.docRefId = docRef.id;
            this.db.collection("payments/"+`${this.userId}`+"/payments").doc(`${this.docRefId}`)
        .onSnapshot((doc) => {
            console.log("Current data: ", doc.data());
            if(doc.data().status === "succeeded"){

                const updatedBalance = this.balance - amount;;

                let writeBatch = firebase.firestore().batch();

                //Update purchased lessons ref
                console.log(this.cartContents); //APEARS AS UNDEFINED

                for(let buyable of this.cartContents){
                    let purchasedLessonsRef = this.afs.firestore.doc(`/purchased_lessons/${this.userId}/lessons/${buyable.b_id}`);
                    writeBatch.update(purchasedLessonsRef, buyable);
                }

                //Update balance ref
                let userBalanceRef = this.afs.firestore.doc(`/users/${this.userId}`);
                writeBatch.update(userBalanceRef, {balance: updatedBalance});

                this.docRefId = null;

                return writeBatch.commit().then(() => {
                    console.log("commiting batch")
                });
            }
        });

        console.log("finished function");
        })
    .catch(error => console.error("Error adding document: ", error))
}
    }

Другой альтернативой являетсясвязать это явно как:

.snapshot(function(doc){
//function body
}).bind(this) // this is binding the correct context (this) to the function
...