Home > Blockchain >  Error Unable to find JSON file in folder, Javascript
Error Unable to find JSON file in folder, Javascript

Time:05-21

Trying to fetch my json file, but apparently the URL is invalid or wrong as per the warning. Tried to console log modelURL and it looks correct. Console.log Output : ./tm-my-image-model/model.json

Full error

Uncaught Error: Request to ./tm-my-image-model/model.json failed 

with status code 404 const URL = ./tm-my-image-model/;

let model, webcam, maxPredictions, happy, sad, angry;
let refresh = true;


    async function init() {
        if (refresh) {
            refresh = false;
            const metadataURL = URL   "metadata.json";
            const modelURL = URL   "model.json";
            
      console.log(modelURL)
}}

File Structure

enter image description here

CodePudding user response:

How are you doing the fetch? (It is not in the code you provided).

In Node.js to work with the file system you need to use the native module fs.

Node.js v18.2.0 documentation File system

CodePudding user response:

This depends where you're calling it from. Currently, the path is relative, and where it's relative to can get quite confusing quite quickly. Note that if you're able to send the path with the page render, that might be good too, since you can then resolve the path from node, and avoid any ajax.

That being said, if possible (since this looks to be node anyway), it would probably be a whole lot easier to use require("x") (or import x from "x" if ESM), or, if you need it to load asynchronously, using the await import("x") syntax.

If you're using server side rendering only, it may be useful to make a network call and fetch the file from your server asynchronously as well.

  • Related