skip to Main Content

How to do I fix this Node.js error.

const express=express();
const app=express();
const fs=require("fs");
const movies=JSON.parse=fs.readFileSync("movies.json", "utf-8"); // this line of code returns error.
app.get("/api/vi/movies", (req, res)=>{
    res.status(200).json({
      status: "success", 
      data: {
        movies: movies
      }
    })
})
app.listen(3000, "127.0.0.1", ()=>{
console.log("Connecting ...");
});

enter image description here

All the code has been written above.

2

Answers


  1. Did you mean:
    const movies=JSON.parse(fs.readFileSync("movies.json", "utf-8"));
    instead of

    const movies=JSON.parse=fs.readFileSync("movies.json", "utf-8");?

    For future reference, you should probably put your error in the post instead of linking to elsewhere.

    Login or Signup to reply.
  2. Parse the data into Json.parse(), which returns from the fs.readFileSync() method.

    const express=express();
    const app=express();
    const fs=require("fs");
    const movies=JSON.parse(fs.readFileSync("movies.json", "utf-8"));
    app.get("/api/vi/movies", (req, res)=>{
        res.status(200).json({
          status: "success", 
          data: {
            movies: movies
          }
        })
    })
    app.listen(3000, "127.0.0.1", ()=>{
    console.log("Connecting ...");
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search