【问题标题】:Mapping data from contentful从内容映射数据
【发布时间】:2019-12-27 21:09:37
【问题描述】:

我一直在关注一个教程系列,并试图推断我迄今为止所做的事情。

我已查询内容以返回其中一种数据类型是 productFeatures 数组的产品详细信息。我试图将它映射到一个列表,并希望它能够迭代出几个 li。但是,我最终得到了一个包含所有条目的 li。谁能帮我理解我在这里误解了什么?

这是我在产品特性组件中的尝试

import React from 'react'
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import './spec.css'

const Spec = ({data}) =>(

    <div>
        <Tabs>
            <TabList>
            <Tab>Key Features</Tab>
            <Tab>Product Documents</Tab>
            </TabList>

            <TabPanel>
                <ul className="spec_list">
                    {data.allContentfulProductPage.edges.map(edge =>(
                        <li className="spec_list__item">{edge.node.productFeatures}</li>
                    ))}

                </ul>
            </TabPanel>
            <TabPanel>
            <h2>Any content 2</h2>
            </TabPanel>
        </Tabs>
    </div>
)

export default Spec

这里是graphql查询返回

{
  "data": {
    "allContentfulProductPage": {
      "edges": [
        {
          "node": {
            "productTitle": "HD Pro Projector M220",
            "productPrice": 299,
            "productDescription": {
              "productDescription": "Designed for use with iPhone*, iPad*, MacBook, and Apple TV, the Miroir HD Projector M220 delivers HD-quality projection for streaming, entertainment, or presentation purposes. Even better, it's small enough to fit in your briefcase or bag. The projector uses a long-lasting LED light source and is built with Texas Instrument's DLP technology, which provides a cinema-quality movie experience.\n"
            },
            "productFeatures": [
              "Texas Instruments DLP Technology",
              "1280 x 720 (720p) native resolution",
              "1080p maximum input resolution",
              "Auto focus with vertical keystone",
              "HDMI input",
              "Viewable screen size from 20 to 100 inches",
              "Built-in lithium-ion battery",
              "Two hours of projecting time",
              "Two built-in 2-watt speakers; audio out jack (3.5mm)"
            ],
            "productImage": [
              {
                "file": {
                  "url": "//images.ctfassets.net/2nhrtgh52wv0/7u8biIRohlsl0OFKBWZ0ti/1dabcec4edd13dee708107929ec8e505/product-shot.jpg"
                }
              },
              {
                "file": {
                  "url": "//images.ctfassets.net/2nhrtgh52wv0/7u8biIRohlsl0OFKBWZ0ti/1dabcec4edd13dee708107929ec8e505/product-shot.jpg"
                }
              }
            ],
            "productSceneImage": {
              "file": {
                "url": "//images.ctfassets.net/2nhrtgh52wv0/6AzIFw9pZDsRtH1JI3A9XR/80ca785ffc60310690e337f7f936a74f/front-room-scene.jpg"
              }
            }
          }
        }
      ]
    }
  }
}

这里是graphql查询返回

export const query = graphql`
  query ProductQuery {
    allContentfulProductPage{
      edges{
        node{
          productTitle
          productPrice
          productFeatures
          productDescription {
            productDescription
          }
        }
      }
    }
  }
`

【问题讨论】:

    标签: reactjs graphql gatsby contentful


    【解决方案1】:

    试试下面的代码。由于edges 是一个数组,并且您正在尝试访问第一个元素以访问productFeatures,因此您需要添加edge[0]

    <ul className="spec_list">
                    {data.allContentfulProductPage.edges[0].node.productFeatures.map(feature =>(
                        <li className="spec_list__item">{feature}</li>
                    ))}
    </ul>
    

    【讨论】:

    • 感谢您的帮助。这产生了“TypeError:无法读取未定义的属性'map'”
    • 你能显示你存储图形查询返回数据的代码吗?
    • 添加到上面
    • 成功了!非常感谢你的队友,非常感谢!
    【解决方案2】:

    const data1={
      "data": {
        "allContentfulProductPage": {
          "edges": [
            {
              "node": {
                "productTitle": "HD Pro Projector M220",
                "productPrice": 299,
                "productDescription": {
                  "productDescription": "Designed for use with iPhone*, iPad*, MacBook, and Apple TV, the Miroir HD Projector M220 delivers HD-quality projection for streaming, entertainment, or presentation purposes. Even better, it's small enough to fit in your briefcase or bag. The projector uses a long-lasting LED light source and is built with Texas Instrument's DLP technology, which provides a cinema-quality movie experience.\n"
                },
                "productFeatures": [
                  "Texas Instruments DLP Technology",
                  "1280 x 720 (720p) native resolution",
                  "1080p maximum input resolution",
                  "Auto focus with vertical keystone",
                  "HDMI input",
                  "Viewable screen size from 20 to 100 inches",
                  "Built-in lithium-ion battery",
                  "Two hours of projecting time",
                  "Two built-in 2-watt speakers; audio out jack (3.5mm)"
                ],
                "productImage": [
                  {
                    "file": {
                      "url": "//images.ctfassets.net/2nhrtgh52wv0/7u8biIRohlsl0OFKBWZ0ti/1dabcec4edd13dee708107929ec8e505/product-shot.jpg"
                    }
                  },
                  {
                    "file": {
                      "url": "//images.ctfassets.net/2nhrtgh52wv0/7u8biIRohlsl0OFKBWZ0ti/1dabcec4edd13dee708107929ec8e505/product-shot.jpg"
                    }
                  }
                ],
                "productSceneImage": {
                  "file": {
                    "url": "//images.ctfassets.net/2nhrtgh52wv0/6AzIFw9pZDsRtH1JI3A9XR/80ca785ffc60310690e337f7f936a74f/front-room-scene.jpg"
                  }
                }
              }
            }
          ]
        }
      }
    }
     console.log(data1.data.allContentfulProductPage.edges.map(elem=>elem.node.productFeatures))

    它适用于 javascript 你确定它不能正常工作

    【讨论】:

    • 所以我得到了所有结果,但不是在他们自己的
    • 中,它们都在一个
    • Texas Instruments DLP 技术 1280 x 720 (720p) 原始分辨率1080p最大输入分辨率
  • 看看我可以在哪里做那件事?
  • 只需点击上面三角形中答案表格旁边的按钮
  • 【解决方案3】:

    我有一个问题要问你,你需要列表包含这个值吗 “德州仪器 DLP 技术”, "1280 x 720 (720p) 原始分辨率", "1080p 最大输入分辨率", “垂直梯形自动对焦”, "HDMI输入", “可视屏幕尺寸从 20 到 100 英寸”, “内置锂离子电池”, “两个小时的投影时间”, “两个内置 2 瓦扬声器;音频输出插孔(3.5 毫米)” 如果是真的

    <ul className="spec_list">
                        {data.allContentfulProductPage.edges.productFeatures.map(edge =>(
                            <li className="spec_list__item">{edge }</li>
                        ))}
    
                    </ul>
    

    【讨论】:

    • 感谢您的帮助。我已经尝试过了,现在我收到错误无法读取未定义的属性“地图”
    • data.allContentfulProductPage.edges.map(elem=>elem.node.productFeatures)
      • {data.allContentfulProductPage.edges.map(elem=>
      • {elem.node.productFeatures }
      • ))}
    • 这与最初的尝试产生了相同的结果
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签