fix: 数组操作添加 ? 可选操作符,避免属性值为 null 时报错

(cherry picked from commit 20f3f72670)
This commit is contained in:
puhui999 2023-06-07 08:59:03 +08:00 committed by shizhong
parent 2d88367e61
commit 9d8e0d507b
2 changed files with 8 additions and 8 deletions

View File

@ -104,7 +104,7 @@ const getDetail = async () => {
formLoading.value = true formLoading.value = true
try { try {
const res = (await ProductSpuApi.getSpu(id)) as ProductSpuApi.Spu const res = (await ProductSpuApi.getSpu(id)) as ProductSpuApi.Spu
res.skus!.forEach((item) => { res.skus?.forEach((item) => {
// //
item.price = formatToFraction(item.price) item.price = formatToFraction(item.price)
item.marketPrice = formatToFraction(item.marketPrice) item.marketPrice = formatToFraction(item.marketPrice)

View File

@ -256,7 +256,7 @@ watch(
return return
} }
copyValueToTarget(formData, data) copyValueToTarget(formData, data)
formData.sliderPicUrls = data['sliderPicUrls'].map((item) => ({ formData.sliderPicUrls = data['sliderPicUrls']?.map((item) => ({
url: item url: item
})) }))
// //
@ -265,16 +265,16 @@ watch(
} }
// skus propertyList // skus propertyList
const properties = [] const properties = []
formData.skus.forEach((sku) => { formData.skus?.forEach((sku) => {
sku.properties.forEach(({ propertyId, propertyName, valueId, valueName }) => { sku.properties?.forEach(({ propertyId, propertyName, valueId, valueName }) => {
// //
if (!properties.some((item) => item.id === propertyId)) { if (!properties?.some((item) => item.id === propertyId)) {
properties.push({ id: propertyId, name: propertyName, values: [] }) properties.push({ id: propertyId, name: propertyName, values: [] })
} }
// //
const index = properties.findIndex((item) => item.id === propertyId) const index = properties?.findIndex((item) => item.id === propertyId)
if (!properties[index].values.some((value) => value.id === valueId)) { if (!properties[index].values?.some((value) => value.id === valueId)) {
properties[index].values.push({ id: valueId, name: valueName }) properties[index].values?.push({ id: valueId, name: valueName })
} }
}) })
}) })