0

First time using Pinia and I'm not a professional developer. Anyway, I defined two stores (XLSXFiles.js contains data loaded from XLSX files, and DAG.js contains an acyclic binary tree). In the 'actions' section of DAG.js, I defined 'initializeTempIndividual' which shall iteratively populate the object 'tempIndividual' with data from a given line of the array 'mappedData[]'. On the first call, the parameter 'index' is set to 0.


    init() {
      this.XLSXfilesInstance = useXLSXfilesStore()
    },

    initializeTempIndividual(index) {
      const recordInXLSX = this.XLSXfilesInstance.mappedData[index]
      this.tempIndividual = {
        indexInTable: index,

/* Here is the definition of XLSXfiles.js */
import { defineStore } from 'pinia'

export const useXLSXfilesStore = defineStore('XLSXfiles' /* the unique ID */, {
  state() {
    return {
      mappedData: [],     // Evénements dans le fichier XLSX

As a result, I get a 'mappedData[index] is undefined' error while I'm still able to output in the console 'this.XLSXfilesInstance.mappedData' which is, as expected, an Array containing the correct data. I'm totally confused.

user8889350
  • 117
  • 9
  • Please, provide https://stackoverflow.com/help/mcve for your problem. The error lacks the context, `this.XLSXfilesInstance.mappedData[index]` line cannot cause it, or it was incorrectly quoted. "On the first call, the parameter 'index' is set to 0." - you'll get undefined if an array is empty, it's not enough to trigger an error but the code probably works not the way you expect – Estus Flask Mar 28 '23 at 08:38
  • In init(): console.log(this.XLSXfilesInstance.mappedData) prints out Proxy(Array)[[Handler]]: Object[[Target]]: Array(300)[0 … 99][100 … 199][200 … 299]length: 300[[Prototype]]: Array(0)[[IsRevoked]]: false Why is the length of the array 0 (console.log('Size of mappedData: ' + this.XLSXfilesInstance.mappedData.length))? DAG.js:59 Size of mappedData: 0 and why first element is undefined (console.log('Element 0 in mappedData: ' + this.XLSXfilesInstance.mappedData[0])? DAG.js:60 Element 0 de mappedData: undefined Seems the console can access to mappedData in init() but not to its elements – user8889350 Mar 28 '23 at 10:18
  • You have race condition and access the array too early. See https://stackoverflow.com/questions/38660832/console-log-of-element-children-shows-0-length-but-has-three-entries-when-expand and linked questions. The question cannot be answered without seeing the whole case – Estus Flask Mar 28 '23 at 10:25
  • That's my understanding too. When I create the DAG.js store in main.js, data are not yet loaded into XLSXfiles.js simply because the file name was not yet provided by the user... Is it possible to ```const DAGStore = useDAGStore()``` later in the code (i.e. in a component) ? – user8889350 Mar 28 '23 at 10:40
  • You can use useDAGStore() in a component, that's how a store is accessed/ Again, please, provide https://stackoverflow.com/help/mcve . It's necessary for other users to be able to embrace your problem. – Estus Flask Mar 28 '23 at 10:59

0 Answers0