首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用normalizr规范嵌套的JSON

用normalizr规范嵌套的JSON
EN

Stack Overflow用户
提问于 2017-10-27 12:47:13
回答 1查看 1.8K关注 0票数 0

我的服务中有以下JSON:

代码语言:javascript
复制
[  
   {  
      "name":"Voter1",
      "id":1,
      "votingCard":{  
         "verificationCodes":[  
            "3I08jA",
            "3x0eyE",
            "2_i69I"
         ],
         "votingCode":"7zCOelDnjfBm7TtFydc4QodgonG",
         "finalizationCode":"jyYu",
         "confirmationCode":"4ACfcpBVH45iAXqg7hJ0tbEe_tV"
      }    
   },
    .....
   {  
      "id":5,
      "name":"Voter5",
      "votingCard":{  
         "verificationCodes":[  
            "2c9I9a",
            "3bEeEa",
            "1gPKx2"
         ],
         "confirmationCode":"4Z7wNG35VR2UMO6-W-0aZVEhbLM",
         "votingCode":"6YQ2x-c8LXJZF05gh3zTajU79ct",
         "finalizationCode":"S0CY"
      }
   }
]

并且希望将其规范化,因此votingCards列表和具有" votingCard“属性的投票者列表引用votingCard by id。

代码语言:javascript
复制
import { normalize, schema } from 'normalizr';

const votingCard = new schema.Entity('votingCard');

const voter = new schema.Entity('voter', {
    votingCard: votingCard,
});

const votersSchema = new schema.Array(voter);


const mutations = {
    SOCKET_SYNCVOTERS: (state, data) => {
        var input = JSON.parse(data);
        const normalizedData = normalize(input, votersSchema);
        console.log(normalizedData);
    },
};

然而,我没有得到我想要的:

为什么会有“未定义”?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-28 00:19:11

我认为您需要在idAttribute实体的选项中指定一个‘votingCard’-问题是,normalizr无法在这些对象中找到一个‘id’字段,因此它们都是以id未定义的形式被拾取,并且在实体中相互覆盖。请参阅:https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#schema

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46975430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档