【发布时间】:2020-05-20 19:11:20
【问题描述】:
我定义了以下架构:
export const user = new schema.Entity('users', {}, { idAttribute: 'username' });
export const galleryImage = new schema.Entity('images', {
user,
});
export const stream = new schema.Entity('streams', {
user,
});
export const thing = new schema.Union({
images: galleryImage,
streams: stream,
}, (input, parent) => (parent.type === 'gallery_image' ? 'images' : 'streams'));
export const post = new schema.Entity('posts', {
user,
thing,
});
export const feedSchema = new schema.Array(post);
基本上,我的帖子方案“事物”是多态的,可以是 GalleryImage 或 Stream。它们是相同的,但我希望能够在标准化后区分它们,这似乎很容易做到。我的问题是,即使我已经定义了idAttribute,它在规范化后嵌套的thing 中是未定义的。
知道为什么会这样吗?
【问题讨论】:
-
您能否添加一些示例数据以便您的代码可以运行?
-
奇怪的是,当我在 Codebox 中尝试它时,它可以正常工作。呵呵。
-
我设置了一个密码箱,但由于某种原因,它在那里工作,而不是我的本地设置:codesandbox.io/s/charming-tu-l57id
-
@PaulArmstrong 使用 JSON.stringify 似乎可以工作一次,但在 React 中不起作用。仍然在渲染中检测为未定义。
-
对不起,console.log 好像骗了我,我的问题的原因是重新选择。