Skip to content

types.Date is giving error. #1696

Answered by EmilTholin
abhiram2600 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @abhiram2600!

It looks like the value you are trying to pass to commentTimeStamp is an object with properties seconds and nanoseconds. You could e.g. add a custom preProcessSnapshot to just take the seconds property and multiply it with 1000 to get milliseconds, and MST will be happy.

const MyModel = types
  .model("MyModel", {
    comments: types.array(
      types.model({
        commentBy: types.string,
        comment: types.string,
        commentTimeStamp: types.Date
      })
    )
  })
  .preProcessSnapshot((snapshot) => ({
    ...snapshot,
    comments: snapshot.comments.map((comment) => ({
      ...comment,
      commentTimeStamp: comment.commentTimeStamp.seconds * 1000
    }))

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by EmilTholin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants