简体   繁体   English

ReactJS - 未处理的拒绝(TypeError):无法读取未定义的属性“id”

[英]ReactJS - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined

Basically I'm creating an e-commerce test site by linking it with stripe and Commercejs.基本上,我通过将其与 stripe 和 Commercejs 链接来创建一个电子商务测试站点。 Everything works up until the checkout form loads.一切正常,直到结帐表格加载。 I am able to input information, however, upon the page being loaded I get the error:我可以输入信息,但是,在加载页面时出现错误:

"Unhandled Rejection (TypeError): Cannot read property 'id' of undefined" for only one line of code. “未处理的拒绝(TypeError):无法读取未定义的属性'id'”仅用于一行代码。 Which is:这是:

setShippingOption(options[0].id);

That code was nested in:该代码嵌套在:

const fetchShippingOptions = async (checkoutTokenId, country, stateProvince = null) => {
   const options = await commerce.checkout.getShippingOptions(checkoutTokenId, { country, region: stateProvince });

setShippingOptions(options);
setShippingOption(options[0].id);
};

This code is essentially for the address form, which follows:这段代码本质上是针对地址形式的,如下:

 import React, { useState, useEffect } from 'react'; import { InputLabel, Select, MenuItem, Button, Grid, Typography } from '@material-ui/core'; import { useForm, FormProvider } from 'react-hook-form'; import { Link } from 'react-router-dom'; import { commerce } from '../../lib/commerce'; import FormInput from './FormInput'; const AddressForm = ({ checkoutToken, next }) => { const [shippingCountries, setShippingCountries] = useState([]); const [shippingCountry, setShippingCountry] = useState(''); const [shippingSubdivisions, setShippingSubdivisions] = useState([]); const [shippingSubdivision, setShippingSubdivision] = useState(''); const [shippingOptions, setShippingOptions] = useState([]); const [shippingOption, setShippingOption] = useState(''); const methods = useForm(); const fetchShippingCountries = async (checkoutTokenId) => { const { countries } = await commerce.services.localeListShippingCountries(checkoutTokenId); setShippingCountries(countries); setShippingCountry(Object.keys(countries)[0]); }; const fetchSubdivisions = async (countryCode) => { const { subdivisions } = await commerce.services.localeListSubdivisions(countryCode); setShippingSubdivisions(subdivisions); setShippingSubdivision(Object.keys(subdivisions)[0]); }; const fetchShippingOptions = async (checkoutTokenId, country, stateProvince = null) => { const options = await commerce.checkout.getShippingOptions(checkoutTokenId, { country, region: stateProvince }); setShippingOptions(options); setShippingOption(options[0].id); }; useEffect(() => { fetchShippingCountries(checkoutToken.id); }, []); useEffect(() => { if (shippingCountry) fetchSubdivisions(shippingCountry); }, [shippingCountry]); useEffect(() => { if (shippingSubdivision) fetchShippingOptions(checkoutToken.id, shippingCountry, shippingSubdivision); }, [shippingSubdivision]); return ( <> <Typography variant="h6" gutterBottom>Shipping address</Typography> <FormProvider {...methods}> <form onSubmit={methods.handleSubmit((data) => next({...data, shippingCountry, shippingSubdivision, shippingOption }))}> <Grid container spacing={3}> <FormInput required name="firstName" label="First name" /> <FormInput required name="lastName" label="Last name" /> <FormInput required name="address1" label="Address line 1" /> <FormInput required name="email" label="Email" /> <FormInput required name="city" label="City" /> <FormInput required name="zip" label="Zip / Postal code" /> <Grid item xs={12} sm={6}> <InputLabel>Shipping Country</InputLabel> <Select value={shippingCountry} fullWidth onChange={(e) => setShippingCountry(e.target.value)}> {Object.entries(shippingCountries).map(([code, name]) => ({ id: code, label: name })).map((item) => ( <MenuItem key={item.id} value={item.id}> {item.label} </MenuItem> ))} </Select> </Grid> <Grid item xs={12} sm={6}> <InputLabel>Shipping Subdivision</InputLabel> <Select value={shippingSubdivision} fullWidth onChange={(e) => setShippingSubdivision(e.target.value)}> {Object.entries(shippingSubdivisions).map(([code, name]) => ({ id: code, label: name })).map((item) => ( <MenuItem key={item.id} value={item.id}> {item.label} </MenuItem> ))} </Select> </Grid> <Grid item xs={12} sm={6}> <InputLabel>Shipping Options</InputLabel> <Select value={shippingOption} fullWidth onChange={(e) => setShippingOption(e.target.value)}> {shippingOptions.map((sO) => ({ id: sO.id, label: `${sO.description} - (${sO.price.formatted_with_symbol})` })).map((item) => ( <MenuItem key={item.id} value={item.id}> {item.label} </MenuItem> ))} </Select> </Grid> </Grid> <br /> <div style={{ display: 'flex', justifyContent: 'space-between' }}> <Button component={Link} variant="outlined" to="/cart">Back to Cart</Button> <Button type="submit" variant="contained" color="primary">Next</Button> </div> </form> </FormProvider> </> ); }; export default AddressForm;

Aside from this, that was the only error I was receiving.除此之外,这是我收到的唯一错误。

Just a side note, I'm still fairly new to JavaScript, I'm in my first year of university.顺便说一句,我对 JavaScript 还是很陌生,我在大学的第一年。

Make sure that the countries in your Commercejs shipping settings have all of the correct regions.确保您的 Commercejs 运输设置中的国家/地区具有所有正确的区域。 I was using the United States but noticed that I only had 2 regions enabled.我使用的是美国,但注意到我只启用了 2 个区域。

Check the shipping setups at dashboard you might be missing the subdivisions检查仪表板上的运输设置,您可能会缺少细分

Edit de United States zone and in add/edit countries make sure to add all of the 57 regions.编辑美国区域并在添加/编辑国家/地区确保添加所有 57 个区域。

Just ran into your problem and fixed it.刚刚遇到您的问题并解决了它。 I also used the same resource as you.我也使用了和你一样的资源。

If you console.log(options) in your fetchShippingOptions const, you'll probably see a description value with Domestic or International.如果您在 fetchShippingOptions const 中使用 console.log(options),您可能会看到国内或国际的描述值。 These are your shipping categories.这些是您的运输类别。

Depending on your CommerceJS setup, one of these is selected by default.根据您的 CommerceJS 设置,默认选择其中之一。 When the default it selected, it tries to load your subdivisions immediately.当它选择默认值时,它会尝试立即加载您的细分。

Here's the problem: if you try to render subdivisions that don't mesh with your backend, it will consider the value undefined .这就是问题所在:如果您尝试渲染不与后端啮合的细分,它将考虑值 undefined So, you have to go to the Shipping Options in your Commerce JS profile and ensure that all subdivisions for every shipping option are available on your account (so that your API key can properly read the back-end values).因此,您必须将 go 设置为 Commerce JS 配置文件中的运输选项,并确保您的帐户中每个运输选项的所有细分都可用(以便您的 API 密钥可以正确读取后端值)。

Do you have a Shipping entry that you forgot to enable all the regions on?您是否有忘记启用所有区域的运输条目? That's what I had.这就是我所拥有的。 Remember, in your form you're mapping all of the subdivisions.请记住,在您的表单中,您正在映射所有细分。 That doesn't necessarily mean that, when you try to push data with your API, all of the back-end endpoints are properly connected.这并不一定意味着,当您尝试使用 API 推送数据时,所有后端端点都已正确连接。

So, go to your Commerce JS account and check your Shipping Options for this project.因此,将 go 发送到您的 Commerce JS 帐户并检查此项目的运输选项。 If any entry shows less Regions than they should, the entry is not properly housing your endpoints and will end up unable to read the values.如果任何条目显示的区域少于应有的区域,则该条目没有正确容纳您的端点,最终将无法读取这些值。

暂无
暂无

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

相关问题 ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined Reactjs + Redux 未处理的拒绝(TypeError):无法读取未定义的属性“数据” - Reactjs + Redux Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript 未处理的拒绝(TypeError):无法读取未定义的属性“ id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined 无法读取未定义未处理拒绝的属性“_id” - Cannot read property '_id' of undefined unhandled Rejection (节点:19502)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性&#39;indexOf&#39; - (node:19502) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'indexOf' of undefined 未处理的拒绝(TypeError):尝试在 ReactJS 上使用 Axios 加载 API 数据时,无法读取未定义的属性“0” - Unhandled Rejection (TypeError): Cannot read property '0' of undefined happening while trying to load API data with Axios on ReactJS 未处理的拒绝(typeError):无法在 google chrome mobile 上使用 reactjs axios 读取未定义的属性 - Unhandled Rejection (typeError): Cannot read property of undefined using reactjs axios on google chrome mobile ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送”。 推送到另一个组件时遇到问题 - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined. Having issues pushing to another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM