1amageek / pring.ts

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

TypeError: collection.setParent is not a function #6

Closed starhoshi closed 6 years ago

starhoshi commented 6 years ago

Error

     TypeError: collection.setParent is not a function
      at User.Base._init (node_modules/pring/src/pring.js:97:32)
      at User.<anonymous> (node_modules/pring/src/pring.js:231:34)
      at step (node_modules/pring/src/pring.js:32:23)
      at Object.next (node_modules/pring/src/pring.js:13:53)
      at node_modules/pring/src/pring.js:7:71
      at new Promise (<anonymous>)
      at __awaiter (node_modules/pring/src/pring.js:3:12)
      at User.Base.save (node_modules/pring/src/pring.js:226:20)
      at Context.<anonymous> (test/order.test.ts:62:24)
      at Generator.next (<anonymous>)
      at fulfilled (test/order.test.ts:6:32)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:160:7)

Code

export class User extends Pring.Base {
  keyValue: { [id: string]: string }
}
const user = new User(authUser.uid)
user.keyValue = {key: 'value'}
user.save() // => TypeError: collection.setParent is not a function

JSON の keyValue 形式だと typeof value === "object" が true になってしまい https://github.com/1amageek/pring.ts/blob/master/pring.ts#L106 でエラーになりました。

ここを if (value instanceof ReferenceCollection || value instanceof NestedCollection) に変えたところ L106 はうまくいきましたが、今度は https://github.com/1amageek/pring.ts/blob/master/pring.ts#L191collection.value is not a function になります。

typeof value === "object" の判定方法がよくなさそうです。

starhoshi commented 6 years ago

thanks! https://github.com/1amageek/pring.ts/commit/362521557b5f07c8476452b0d9ba9ceba1f13ce5