mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

not assignable to type: 'model' (Value is not a plain object) #1973

Closed fahry-mohammed closed 1 year ago

fahry-mohammed commented 1 year ago

Question

I am working on the react native mobile app. The mobile application uses firebase. So according to the collections and documents, I have created models. But when I receive the timestamp from firebase the MobX throws an error as follows:

at path "/details/createdAt" snapshot '{"seconds":1665659691,"nanoseconds":836000000}' is not assignable to type: 'timeModel' (Value is not a plain object), expected an instance of 'timeModel' or a snapshot like '{ seconds: number?; nanoseconds: number? }?' instead.]

To this I have created a model as follows:

Child Model

export const timeModel = types
  .model("timeModel", {
    seconds: types.optional(types.number, 0),
    nanoseconds: types.optional(types.number, 0),
  })

Base Model

export const detail = types
  .model("detailModel", {
    createdAt: types.optional(timeModel, {}),
  })

Json starts like this:

{"createdAt": {"nanoseconds": 836000000, "seconds": 1665659691}, "createdBy":...

This throws the above error and I couldn't able think of what I went wrong! Are you guys see any problem? Thanks!

fahry-mohammed commented 1 year ago

I have created a custom type for firebase.firestore.Timestamp and solved the issue.