javascript - Normalizr - 未达到预期结果

标签 javascript reactjs redux flux normalizr

import { normalize, Schema, arrayOf } from 'normalizr';

var ListA = [
    {
        id:1, 
        text: "text1",
        comments : [
            {
                id: 232,
                text: "asfasd"
            },
            {
                id: 333,
                text: "abcsss"
            }
        ]
    }, 
    {id:2, text:"text2", comments:[]}, 
    {id:3, text:"text3", comments:[]}
    ]

我正在尝试使这个简单的响应正常化。我不确定我在做什么有什么问题,或者我没有理解 normalizr 文档。

const post = new Schema('posts');
// const comment = new Schema('comments');
// const collection = new Schema('collections');

// post.define({
//  comments : comment,
//  collections : arrayOf(collection)
// });
ListA = normalize(ListA, {
    posts: arrayOf(post)
});

console.log(ListA);

这只会对“结果”对象产生相同的响应,而实体对象是空的。有人可以帮帮我吗。首先,我试图仅对帖子进行规范化,然后再对帖子进行规范化。但我无法跨过第一步。

最佳答案

使用normalizr的几个例子

1) 归一化简单对象
举个例子

import { Schema, arrayOf } from 'normalizr';
const postSchema = new Schema('posts');

// simple post object
var post = {
  id:1,
  title: "some title"
};

console.log(normalize(post, postSchema));

结果会是

{
   "entities":{
      "posts":{
         "1":{
            "id":1,
            "title":"some title"
         }
      }
   },
   "result":1
}

2) 标准化对象数组

import { Schema, arrayOf } from 'normalizr';
const postSchema = new Schema('posts');

// array of posts
var posts = [
  {
    id:1,
    title: "foo"
  },
  {
    id:2,
    title: "far"
  },
  {
    id:3,
    title: "baz"
  }
];

console.log(normalize(posts, arrayOf(postSchema)));

结果会是

{
   "entities":{
      "posts":{
         "1":{
            "id":1,
            "title":"foo"
         },
         "2":{
            "id":2,
            "title":"far"
         },
         "3":{
            "id":3,
            "title":"baz"
         }
      }
   },
   "result":[
      1,
      2,
      3
   ]
}

3) 规范化复杂对象

const postSchema = new Schema('posts');
const authorSchema = new Schema('authors');
const commentSchema = new Schema('comments');

postSchema.define({
  author: authorSchema,
  comments: arrayOf(commentSchema)
});

// complex post object
var post = {
  id:1,
  title: "foo",
  author: {
    id: 201,
    name: "Bar Baz"
  },
  comments: [
    {
      id: 1002,
      body: "Some content"
    },
    {
      id: 1003,
      body: "Some content 1003"
    },
    {
      id: 1004,
      body: "Some content 1004"
    }
  ]
};

console.log(normalize(post, postSchema));

结果会是

{
  "entities":{
    "posts":{
      "1":{
        "id":1,
          "title":"foo",
          "author":201,
          "comments":[
          1002,
          1003,
          1004
        ]
      }
    },
    "authors":{
      "201":{
        "id":201,
          "name":"Bar Baz"
      }
    },
    "comments":{
      "1002":{
        "id":1002,
          "body":"Some content"
      },
      "1003":{
        "id":1003,
          "body":"Some content 1003"
      },
      "1004":{
        "id":1004,
          "body":"Some content 1004"
      }
    }
  },
  "result":1
}

所以你可以试试

ListA = normalize(ListA, arrayOf(post));

代替

 ListA = normalize(ListA, {
    posts: arrayOf(post)
 });

关于javascript - Normalizr - 未达到预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35470393/

相关文章:

javascript - Meteor Accounts.emailTemplates.resetPassword.html

javascript - 如何设计一个监听 WebSocket 并与 CSS 动画交互的 ReactJS 组件

reactjs - Ant-design Spinner 不工作

javascript - 如何在单击时使选择菜单中的特定选项变灰?

javascript - Ace 编辑器和 vim 键绑定(bind) : using :w command

javascript - 用于生产的 uglify/webpack 之后的每个类的 Class.name 始终为 'e'

javascript - Angular 6 测试 Jasmine Karma : Override Provider Not working

javascript - 未捕获的 TypeError : this. state.map 不是函数

reactjs - Redux 默默地不渲染 connect()ed 组件

javascript - 使用 React Native 和 Redux 热重载 reducer 时堆栈溢出