1

I'm using p5. I've written a function outside of the preload() function and called it within the preload() function. The function looks like this:

const logStuff = () => {

/*37:*/ var JSONfile = loadJSON('data.json')
/*38:*/ console.log(JSONfile)
/*39:*/ let testLat = JSONfile.Sheet1[0].latitude
/*40:*/ let testLong = JSONfile.Sheet1[0].longitude

}

The console reads like this:

v {}
v Sheet1: Array(50)

   0: {state: "Alaska", latitude: 61.385, longitude: -152.2683}
   1:...49:

index.js:39 Uncaught (in promise) TypeError: Cannot read property '0' of undefined
    at createStateButtonObject (index.js:39)
    at preload (index.js:71)
    at p5._start (p5.js:51283)
    at new p5 (p5.js:51639)
    at _globalInit (p5.js:50603)
createStateButtonObject @ index.js:39
preload @ index.js:71
_start @ p5.js:51283
p5 @ p5.js:51639
_globalInit @ p5.js:50603
Promise.then (async)
56.../core/main @ p5.js:50628
o @ p5.js:34
(anonymous) @ p5.js:38
43../accessibility/color_namer @ p5.js:43475
o @ p5.js:34
r @ p5.js:51
(anonymous) @ p5.js:55
(anonymous) @ p5.js:18
(anonymous) @ p5.js:20

What am I doing wrong?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    The [docs](https://p5js.org/reference/#/p5/loadJSON) suggest "Calling `loadJSON()` inside `preload()` guarantees to complete the operation before `setup()` and `draw()` are called." and "Outside of `preload()`, you may supply a callback function to handle the object" with an example. Where is `logStuff` located? Can you provide a [mcve] and explain whether you want the data to be available in `draw` or are you using it for some other purpose? Thanks. – ggorlen Feb 21 '21 at 03:35

0 Answers0