0

I need to access a local file, but when I import it and attempt to display it, it shows [object Object] or [object Promise].

The fact that it's a JSON file isn't important, I tried it with a plain text file.

Importing it normally with import Data from './Data.json' or import * as Data from './Data.json' doesn't work, along with using Fetch to get it, with let Data = fetch('./Data.json'). The first displays [object Object], the second breaks the entire page, and the third displays [object Promise].

matthew
  • 3
  • 4

1 Answers1

1

The string representation of an object will be [object Object]. If your JSON has any meaningful complexity (i.e. it is not just a primitive) that is what you should expect if you try to just throw it at the page.

If you want to check the contents, log the object to console or convert it again first using JSON.stringify.

(Svelte also has {@debug ...} for logging data; this also acts as a breakpoint.)

REPL example

H.B.
  • 166,899
  • 29
  • 327
  • 400