如何正确地将DateTime字符串通过MOBX-State-Tree将日期对象转换为日期对象
有传入的字符串数据是日期时间格式,
其映射此对象罚款;
import { types } from 'mobx-state-tree'
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.string),
})
在路径“/0/warded_at”值” 2022-01-25T21:07:30.473502+00:00:00“
无法分配给类型:(date | null)
,
但是由于它的日期尝试此模型只是将字符串更改为日期;
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.Date),
})
然后在设置对象时获得此错误;
在路径“/0/warded_at”值
” 2022-01-25T21:07:30.473502+00:00:00“
是 不能分配给类型:(日期| null)
因此如何将传入的字符串数据映射到日期对象
there is incoming string data which is date time format,
Its mapping this object fine;
import { types } from 'mobx-state-tree'
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.string),
})
at path "/0/awarded_at" value "2022-01-25T21:07:30.473502+00:00"
is not assignable to type: (Date | null)
but since its datetime trying this model just changed string to date;
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.Date),
})
then get this error in setting object;
at path "/0/awarded_at" value
"2022-01-25T21:07:30.473502+00:00"
is
not assignable to type:(Date | null)
So how to map incoming string data to Date object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种常见的技术是像您在第一个片段中所做的那样将其建模为字符串,创建一个创建
date
的视图,然后使用它:如果您不喜欢该方法上方您可以创建
自定义
类型:One common technique is to model it as a string like you have done in your first snippet, create a view that creates a
Date
out of it, and use that instead:If you don't like the approach above you can create a
custom
type: