简体   繁体   English

设置变体选项数组时出错无法读取未定义的属性“0”

[英]error while setting variants option array Cannot read property '0' of undefined

hello iam creating an ecomerce shop in my chec dasboard i set variants of sizes large small but when i try to implement options drop down on options array i get the error Cannot read property '0' of undefined in line12 which is let finalSizeArray = props.product.variants[0].options.map(option =你好,我在我的 chec 仪表板上创建了一个电子商务商店,我将大小的变体设置为大小,但是当我尝试在选项数组上实现选项下拉菜单时,我收到错误无法读取第 12 行中未定义的属性“0”,这让 finalSizeArray = props。 product.variants[0].options.map(option =

productcard.js产品卡.js

import React, { useState, useEffect } from 'react';
import { Card, Image, Button, Icon, Dropdown } from 'semantic-ui-react';

const ProductCard = (props) => {
     console.log(props.product, 'props from Container')

    const [sizes, setSizes] = useState([])
    const [variantInfo, setVariantInfo] = useState()

    useEffect(() => {        
        
       let finalSizeArray = props.product.variants[0].options.map(option => {
            let sizeInfo = {}

           sizeInfo.key = option.name
           sizeInfo.text = option.name
           sizeInfo.value = option.id

            return sizeInfo
        })

        setSizes(finalSizeArray)
    }, [])

    const handleSize = (e, {value}) => {
        setVariantInfo({[props.product.variants[0].id]: value})
    }

    const handleButtonAddCart = e => {
        e.preventDefault()
        props.addToCart(props.product.id, variantInfo)
        
        
        // Funtion to Clear Select Input for Dropdown - Needs work. 
        // let selectInput = document.querySelectorAll('.sizes-drop')
        // selectInput.forEach((input,i) => {
        //     input.children[0].innerHTML = 'Select Size'
        //     // input.children[0].classList.add('default')
        // })
    }

    return (
        <Card>
            <Image src={props.product.media.source} />
            <Card.Content>
                <Card.Header>{props.product.name}</Card.Header>
                <Card.Meta>{props.product.price.formatted_with_symbol}</Card.Meta>
                <Card.Description>{props.product.description.replace(/(<([^>]+)>)/ig,"")}</Card.Description>
                <Dropdown
                    className="sizes-drop"
                    
                    
                   fluid
                   placeholder='Select Size'
                   selection
                   options={sizes}
                />
                <Button fluid className='add-button' onClick={handleButtonAddCart}>
                    Add to Cart
                    <Icon name='arrow right' />
                </Button>
            </Card.Content>
        </Card>
    );
};

export default ProductCard;

I thinks its because your props not yet loaded... You can give handle to your code like this我认为这是因为你的道具​​还没有加载......你可以像这样处理你的代码

useEffect(() => {        
   let finalSizeArray = props?.product?.variants[0]?.options?.map(option => {
      let sizeInfo = {}

      sizeInfo.key = option.name
      sizeInfo.text = option.name
      sizeInfo.value = option.id

      return sizeInfo
   })

   setSizes(finalSizeArray)
}, [])

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

相关问题 MobX可观察值的设置错误-无法读取未定义的属性过滤器 - MobX Setting of observables error - cannot read property filter of undefined 尝试映射数组时出现错误。 TypeError:无法读取未定义的属性“ map” - Getting error while trying to map over array. TypeError: Cannot read property 'map' of undefined 在角度循环遍历数组时无法读取未定义的0属性 - cannot read property of 0 undefined while looping through array in angular 无法读取数组中未定义的属性“0” - Cannot read property "0" of undefined in array 无法读取未定义的JQuery选项标签的属性 - Cannot Read Property of Undefined JQuery Option Tag 加载路线时出错,无法读取未定义的属性 - error while loading route, cannot read property of undefined 加载数据时出错 TypeError:无法读取未定义的属性“管道” - Error while loading the data TypeError: Cannot read property 'pipe' of undefined 使用Fetch API时无法读取未定义错误的属性 - Cannot read property then of undefined error while using Fetch API 单元测试时出错 - TypeError:无法读取未定义的属性&#39;cityWeather&#39; - Error while unit test - TypeError: Cannot read property 'cityWeather' of undefined 保存数据时出错“无法读取未定义的属性'get'” - Error While Saving Data “Cannot read property 'get' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM