语境
我有一个由 mobx-state-tree (MST) 支持的 React 应用程序。其中一个页面进行两个并行API 调用以获取Projects
system 列表和列表Users
。
然后将这两个 API 的响应作为快照应用到同一棵树中的两个兄弟节点。Project 模型的 owner 字段是指 User 模型。
我的问题是 GET 项目 API 调用通常比 GET 用户 API 调用更早完成。经典的“比赛条件”。因此,当 MST 尝试取消引用所有者值时,具有此类标识符的用户在树中不可用,并且我收到如下运行时错误:
错误:[mobx-state-tree] 无法解析引用“dtGNbVdNOQIAX0OMGaH2r2oSEf2cMSk9AJzPAf4n7kA”以键入“用户”(来自节点:/projectListStore/projects/0/owner)
问题
如何解决该错误?
请注意,使 API 调用相互依赖(await
或users.load().then(users => { projects.load(); }
)不是我们项目的选项。
MST 型号
export const ProjectModel = types
.model('Project', {
projectId: types.optional(types.identifierNumber, 0),
title: '',
descriptionStatus: '',
projectStatus: '',
startDate: CustomDate,
endDate: CustomDate,
owner: types.maybeNull(types.reference(User)), // -----
}) \
// ... |
|
export const User = types |
.model('User', { /
userId: types.identifier, // <<<--
userName: '',
firstName: '',
lastName: '',
email: '',
lastVisited: CustomDate,
})
// ...
API 示例
GET
https://service.com/api/users
[
{
"userId": "g-gqpB1EbTi9XGZOf3IkBpxSjpAHM8fpkeL4YZslEL8",
"userName": "john.doe@service.com",
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@service.com",
"lastVisited": "2018-01-18T17:32:48.947"
},
{
...
},
...
]
GET
https://service.com/api/workmanagement/projects?minStartDate=&maxStartDate=2018-11-24&status=Active
[
{
"projectId": 1,
"title": "Project Title",
"description": "Praesent luctus vitae nunc sed condimentum. Duis maximus arcu tortor, vitae placerat enim ultrices sed. Etiam scelerisque gravida justo, vitae venenatis dui pellentesque eu.",
"projectStatus": "active",
"startDate": "2018-08-20T00:00:00",
"endDate": "2019-07-01T00:00:00",
"owner": "g-gqpB1EbTi9XGZOf3IkBpxSjpAHM8fpkeL4YZslEL8",
},
{
// ...
},
...
]