Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
Nuxt 3: Pinia: ¿por qué mi mutación mi estado destruye toda la matriz?

aqui esta mi codigo

 import { defineStore } from "pinia"; export const DB_CART = defineStore("CART", { state: () => ({ CART: [ { $id: "62616ffc6d13e2a9eb04", quantity: 1 }, { $id: "6261711719aa15827836", quantity: 1 }, { $id: "6275c020740fbd04db50", quantity: 1 } ], }), actions: { // INCREMENT PRODUCT QUANTITY IN CART incrementCartQuantity(id) { const cartItem = this.CART.find(item => item.$id = id); cartItem.quantity++; }, // DECREMENT PRODUCT QUANTITY IN CART decrementCartQuantity(id) { const cartItem = this.CART.find(item => item.$id = id); if (cartItem.quantity === 1) return cartItem.quantity--; }, // ADD PRODUCT TO CART addToCart(item) { this.CART.push(item) } }, persist: true })

Me gustaría entender por qué cuando aumento o disminuyo un artículo del carrito a partir del segundo en la matriz, la matriz se reorganiza.

 incrementCartQuantity(id) { const cartItem = this.CART.find(item => item.$id = id); cartItem.quantity++; console.log(this.CART) /*EXPECTED OUTPUT [ { $id: "62616ffc6d13e2a9eb04", quantity: 1 }, { $id: "6261711719aa15827836", quantity: 1 }, { $id: "6275c020740fbd04db50", quantity: 1 } ] *//*RETURED OUTPUT [ { $id: "6261711719aa15827836", quantity: 2 }, { $id: "6261711719aa15827836", quantity: 1 }, INCREMENTED OBJECT { $id: "6275c020740fbd04db50", quantity: 1 } ] */ },

en mi componente Vue, en incrementCartItem, descubrí que toda la matriz se baraja y aparece la que está en la parte superior, y la reemplaza con el elemento que se está mutando. No creo que la matriz deba verse afectada de ninguna manera. .

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su método de find está haciendo una asignación " = " en lugar de una comparación " === ".

Reemplazar

 this.CART.find(item => item.$id = id);

con

 this.CART.find(item => item.$id === id);

en ambos métodos de carro.

Su find asigna la identificación buscada a la $id id primer elemento, cada vez que se ejecuta.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!