简体   繁体   English

我如何映射对象的嵌套数组,其中包含对象的嵌套数组在react,redux中

[英]how do I map over a nested array of objects which contains a nested array of objects in react, redux

I have a mongodb document that I'm pulling with an axios ajax call. 我有一个要通过axios ajax调用提取的mongodb文档。 The document has an array of objects which also contains a nested array of objects. 该文档具有一个对象数组,该对象数组还包含一个嵌套的对象数组。 Every object has a mongo id assigned to it. 每个对象都有一个mongo ID分配给它。 The top most data is displayed in the corresponding top most Presentation component, but subsequent arrays of objects are not displayed in their relative Presentation components. 最高数据显示在相应的最高Presentation组件中,但后续的对象数组未显示在其相对的Presentation组件中。 The error I am getting is. 我得到的错误是。 "TypeError: this.props.card.rdfts is undefined" , yet it's non array siblings are displayed. “ TypeError:this.props.card.rdfts未定义” ,但显示的是非数组同级。 Yet, when I look in react dev tools and redux dev tools the array of objects is infact there and as an array of objects which contains an array of objects. 但是,当我查看react dev工具和redux dev工具时,对象数组实际上是存在的,并且是包含对象数组的对象数组。

I've searched countless stackoverflow artiles as well as countless github articles. 我搜索了无数的stackoverflow构件以及无数的github文章。 I've tried many different approaches but nothing seems to work. 我尝试了许多不同的方法,但是似乎没有任何效果。

My mongodb docuent is... 我的mongodb文档是...

{
"_id" : ObjectId("58d93a23a62fdfa37438b4ca"),
"url" : "/api/Rationale/Restraint/Model/1",
"type" : "body",
"title" : "title",
"vid" : {
    "_id" : ObjectId("58d93a23a62fdfa37438b4c3"),
    "type" : "video",
    "src" : "somesrc",
    "border" : "1",
    "width" : "400",
    "height" : "200",
    "frameborder" : "0",
    "allowfullscreen" : false
},
"topic" : "Topic?",
"rdfts" : [ 
    {
        "_id" : ObjectId("58d93a23a62fdfa37438b4c4"),
        "rdft" : "ReasonDetailFactTransition",
        "explainations" : [ 
            {
                "_id" : ObjectId("58d93a23a62fdfa37438b4c5"),
                "explaination" : "Explaination1"
            }, 
            {
                "_id" : ObjectId("58d93a23a62fdfa37438b4c6"),
                "explaination" : "Explaination2"
            }
        ]
    }, 
    {
        "_id" : ObjectId("58d93a23a62fdfa37438b4c7"),
        "rdft" : "Reason Detail Fact Transition 2",
        "explainations" : [ 
            {
                "_id" : ObjectId("58d93a23a62fdfa37438b4c8"),
                "explaination" : "Explaination1"
            }, 
            {
                "_id" : ObjectId("58d93a23a62fdfa37438b4c9"),
                "explaination" : "Explaination2"
            }
        ]
    }
],
"conclusion" : "Conclusion of Card"

} }

I'm making my ajax call from componentwillmount 我正在从componentwillmount进行ajax调用

componentWillMount() {
    console.log("componentDidMount")
    let myURI = ''; {
        let { api, page, section, subsection, card } = this.props.match.params;
        myURI = `/${api}/${page}/${section}/${subsection}/${card}`;
    }
    console.log("json call 1")
    this.props.dispatch(asyncSetCard(myURI));

}

This is my thunk that doesn't seem to be halting flow of control. 这是我的重击,似乎并没有阻止控制流。

asyncSetCard(uri) {
  return dispatch => {

    return axios.get(uri).then(response => {

      dispatch(setCardSuccess(response.data[0]));

    }).catch(error => {

      throw (error);
    });
  }
}

So the ajax call seems to work but when I try to render nested arrays of objects with other nested arrays of objects I get the above TypeError. 因此,ajax调用似乎有效,但是当我尝试使用其他嵌套对象数组渲染对象的嵌套数组时,出现上述TypeError。

This happens in the render function 这发生在渲染功能中

render() {
    return (
        <div>
            <h2>Title: {this.props.card.title}</h2>
            <h3>Topic: {this.props.card.topic}</h3>
                       {this.props.card.rdfts.map(RDFTs)}
            <h3>Conclusion: {this.props.card.conclusion}</h3>
        </div>
    );
}

If I delete {this.props.card.rdfts.map(RDFTs)} then the first level of my documents is displayed. 如果删除{this.props.card.rdfts.map(RDFTs)} ,则会显示文档的第一级。 If I put this back in, then subsequent nested arrays of documents do not get displayed and I get the following error. 如果将其放回去,则后续的嵌套文档数组将不会显示,并且会出现以下错误。 "TypeError: this.props.card.rdfts is undefined" “ TypeError:this.props.card.rdfts未定义”

I've tried to work with 1 level of array of objects at a time but that doesn't seem to work ether. 我试图一次处理1级对象数组,但这似乎无法正常工作。

Also, It doesn't seem that my axios promise is stopping halting flow of control. 另外,看来我的axios承诺似乎没有停止控制流。 I originally wanted for the promise to be resolved then render but it seems that the then just falls through and render is called immediately. 我最初希望将诺言解决,然后进行渲染,但似乎仅此而已,立即调用了渲染。 I wonder if the array of objects isn't instantiated in time or what but it doesn't seem to work. 我想知道对象数组是否未及时实例化,还是什么实例化,但似乎不起作用。

If you take a look at my console you will see that I'm getting there error before the promise is resolved, but that shouldn't matter since react is reading straight from the state tree. 如果您看一下我的控制台,您会发现在兑现承诺之前我到了那里,但那没关系,因为反应是直接从状态树中读取。 Once the state tree is updated, then react is supposed to update. 一旦状态树被更新,那么反应应该被更新。 right? 对?

15:40:24.978 GET mybuldleyadabundle.js [HTTP/1.1 200 OK 117ms] 15:40:24.978 GET mybuldleyadabundle.js [HTTP / 1.1 200 OK 117ms]

15:40:26.855 componentDidMount CardCC.jsx:42:13 15:40:26.855 componentDidMount CardCC.jsx:42:13

15:40:26.867 TypeError: this.props.card.rdfts is undefined[Learn More] ... 15:40:26.867 TypeError:this.props.card.rdfts未定义[了解更多] ...

15:40:27.327 GET XHR myhttpurl.herokuapp.com/api/Rationale/Restraint/Model/1 [HTTP/1.1 200 OK 143ms] 15:40:27.327 GET XHR myhttpurl.herokuapp.com/api/Rationale/Restraint/Model/1 [HTTP / 1.1 200 OK 143ms]

15:40:27.800 GET XHR mylocalhost:3001/sockjs-node/info [HTTP/1.1 200 OK 2ms] 15:40:27.800 GET XHR mylocalhost:3001 / sockjs-node / info [HTTP / 1.1 200 OK 2ms]

Dan Abramov already figured this out. 丹·阿布拉莫夫(Dan Abramov)已经弄清楚了。 https://github.com/paularmstrong/normalizr . https://github.com/paularmstrong/normalizr Normalizer is what I needed. 我需要标准化器。 :) Thanks Dan.Also had help from Cory. :)谢谢Dan。也得到了Cory的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM