Javascript 我在本地存储中添加的其他字段在购物车中被覆盖
我正在用React和Node js构建一个电子商务网站。我的本地存储和购物车有问题。我已将一些字段添加到本地存储(大小和完成),您可以在产品页面上选择这些字段: 这是具有该功能的产品示例: 这是将项目添加到购物车的函数Javascript 我在本地存储中添加的其他字段在购物车中被覆盖,javascript,node.js,reactjs,local-storage,e-commerce,Javascript,Node.js,Reactjs,Local Storage,E Commerce,我正在用React和Node js构建一个电子商务网站。我的本地存储和购物车有问题。我已将一些字段添加到本地存储(大小和完成),您可以在产品页面上选择这些字段: 这是具有该功能的产品示例: 这是将项目添加到购物车的函数 export const addItem = (item, count, finish, size, next) => { let cart = [] if (typeof window !== 'undefined') { if(loca
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}
但是,addItem函数中的代码块:
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
这将阻止您添加相同的项目,并覆盖具有相同产品id的项目。(如果您选择Honed Arabescato并将其添加到购物车,然后返回并选择抛光Arabescato,您将只看到抛光版本)。我正在尝试找出如何将两者添加到购物车中。如果我注释掉上面的代码^,同一个项目会多次添加到购物车中,并且所有项目都会被覆盖。我如何才能将不同的变体添加到购物车中
我的代码可以在这里看到:
(前端)
(后端)
我已经试着对新设置进行注释,因为这只允许将该产品的一个副本添加到购物车阵列中。理想情况下,我希望能够添加到购物车相同的产品与不同的完成
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}
通过在addItem函数中对Array.from块进行注释,我希望将产品的不同版本添加到购物车中。我没想到它会覆盖购物车中的产品。我看不出您在哪里设置id字段。null===null是否可能是您的唯一匹配项
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}
export const addItem = (item, count, finish, size, next) => {
let cart = []
if (typeof window !== 'undefined') {
if(localStorage.getItem('cart')) {
cart = JSON.parse(localStorage.getItem("cart"))
}
cart.push({
...item,
count: count,
finish: finish,
size: size
})
cart = Array.from(new Set(cart.map(p => p._id))).map(id => {
console.log(cart)
return cart.find(p => p._id === id)
})
localStorage.setItem("cart", JSON.stringify(cart))
next()
}
}