1amageek / pring.ts

Cloud Firestore model framework for TypeScript - Google
https://firebase.google.com/docs/firestore/
MIT License
108 stars 7 forks source link

NestedCollection's forEach is not working #10

Open starhoshi opened 6 years ago

starhoshi commented 6 years ago
export class Cart extends Pring.Base {
  @property cartSKUs: Pring.NestedCollection<CartSKU> = new Pring.NestedCollection(this)
}

export class CartSKU extends Pring.Base {
  @property quantity: number
}
      const cart = new Firebase.Cart()
      cart.init(cartSnapshot)

      console.log(cart.cartSKUs.objects) // => []
      cart.cartSKUs.forEach(c => {
        console.log('c.id', c.id)
      })

      const cartSKUs = await cart.cartSKUs.getReference().get().then(s => s.docs)
      cartSKUs.forEach(c => {
        console.log(c.id) // => logged cartSKU id
      })

Collection の forEach を実行しても、 objects が [] なので for が周りません。 firestore を直で叩くと cartSKU 情報は取得できているので、情報は存在します。

starhoshi commented 6 years ago
      const cartSKUs: Firebase.CartSKU[] = await cart.cartSKUs.getReference().get().then(s => {
        return s.docs.map(doc => {
          const cartSKU = new Firebase.CartSKU()
          cartSKU.init(doc)
          return cartSKU
        })
      })

クライアント側でこれをやればいいので pring.ts 側でサポートする必要はない気がしてきた 🤔