Unable to denormalize entity with the new Normalizr 3.1.0

梦想与她 提交于 2019-12-24 10:56:57

问题


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

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 }

denormalizing

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

this.props.users.entities

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

EDIT: 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)

回答1:


This line is wrong:

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

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

[edit]

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



来源:https://stackoverflow.com/questions/41828649/unable-to-denormalize-entity-with-the-new-normalizr-3-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!