无法使用新的Normalizr 3.1.0对实体进行非规范化 [英] Unable to denormalize entity with the new Normalizr 3.1.0

查看:125
本文介绍了无法使用新的Normalizr 3.1.0对实体进行非规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户规范化为一个完整的对象.我正在尝试根据文档这里.据我了解,输入应该是结果值之一,然后您提供架构和实体来处理重建对象.不过,返回的值是一个对象,该对象的键为0,值为输入.例如{0:44},而不是整个非规范化用户对象.

I'm trying to denormalize a user back into a complete object. I'm trying to follow based on the docs here. From what I understand the input should be one of the result values, and then you provide the schema and entities to process rebuilding the object. The returned value though is an object that has a key of 0 and a value of the input. {0:44} for example, instead of the whole denormalized user object.

userSchemas.js

userSchemas.js

import { schema } from 'normalizr';

const photos = new schema.Entity('photos')
const phones = new schema.Entity('phones')
const user = new schema.Entity('users', {
    photos: [photos],
    phones: [phones]
})

const usersSchema = new schema.Array(users)

export { usersSchema, user as userSchema }

去规范化

denormalize([user], userSchema, this.props.users.entities)

this.props.users.entities

this.props.users.entities

{
users : { 
         1: {id:1, name: "john", phones:[2]}
        },
photos : {},
phones : {
          2: {id:2, phone: "34234324"}
         }
}

可以粘贴到在控制台中显示问题的 http://requirebin.com 的要旨

Gist that can be pasted into http://requirebin.com that shows the issue in console

var normalizr = require("normalizr")

const photos = new normalizr.schema.Entity('photos')
const phones = new normalizr.schema.Entity('phones')
const user = new normalizr.schema.Entity('user', {
    photos: [photos],
    phones: [phones]
})

const usersSchema = new normalizr.schema.Array(user)

var users = [
  {id: 1, name: "bob", phones: [{id:3, phone: 45234324},{id:4, phone: 42342432}]},
    {id: 2, name: "will", phones: [{id:3, phone: 45234324},{id:6, phone: 5435345}]},
    {id: 4, name: "sam", phones: [{id:6, phone: 5345353},{id:7, phone: 42342432}]}
]

var normalizedUsers = normalizr.normalize(users, usersSchema)

var denormalizedUser = normalizr.denormalize([2], user, normalizedUsers)

console.log(denormalizedUser)

推荐答案

此行是错误的:

var denormalizedUser = normalizr.denormalize([2], user, normalizedUsers)

第一个参数应为2或第二个参数应为[ user ].

Either your first argument should just be 2 or your second argument should be [ user ].

此外,normalizedUsers应该只是entities,而不是完整的标准化结果.使用normalizedUsers.entities

Also, normalizedUsers should be just the entities, not the full normalized result. Use normalizedUsers.entities

这篇关于无法使用新的Normalizr 3.1.0对实体进行非规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆