jeudi 19 novembre 2020

Error reading file from disk: Error: ENOENT: no such file or directory, open './database.json

Im doing some tests, where I'm testing the POST routes,I think the test are written out fine, however now i'm getting an error which says

"Error reading file from disk: Error: ENOENT: no such file or directory, open './database.json"

describe ("POST /post methods",() => {
  it("should get /post ",(done)=>{
     const incomingRequest = {
        name: "charanjit",
        content: "im posting",
        gif: ""

     };
    chai.request(server)
    .post("/posts")
    .send(incomingRequest)
    .end(function (err, res) {
      expect(res).to.have.status(500)
      done()
    });


    
    
    


})
it("should get /comments ",(done)=>{
    const commente = {
       comment: "hi omg"

    };
    chai.request(server)
    .post("/comments")
    .send(commente)
    .end(function (err, res) {
      expect(res).to.have.status(200)
      done()
    });


 })
   it("should get /emoji ",(done)=>{
    const commente = {
    comment: "hi omg"

 };
 chai.request(server)
 .post("/comments")
 .send(commente)
 .end(function (err, res) {
  expect(res).to.have.status(200)
  done()
  });

These are the test i've wriiten. and this is what I wrote on the server side code.

 server.post("/posts", (req, res) => {
  const incomingRequest = req.body;
  if (isValidPost(incomingRequest)) {
    const post = {
   name: incomingRequest.name.toString(),
   content: incomingRequest.content.toString(),
   giph: incomingRequest.gif.toString(),
   date: new Date(),
   likes: 0,
   dislikes: 0,
   laughs: 0,
   comments: [],
  };

  //read the file
  fs.readFile("./database.json", "utf8", (err, data) => {
  if (err) {
    console.log(`Error reading file from disk: ${err}`);
  } else {
    //parse JSON string to JSON object
    const postsData = JSON.parse(data);
    post.id = postsData.length + 1;
    postsData.push(post);

    fs.writeFile(
      "./database.json",
      JSON.stringify(postsData, null, 4),
      (err) => {
        if (err) {
          console.log(`Error writing file: ${err}`);
        }
      }
    );
    res.status(201).send(post);
    }
  });
 }    else {
  res.status(422).send("Name and content required!");
 }
});

  //POST ROUTE - EMOJIS

  server.post("/emojis", (req, res) => {
 const incomingRequest = req.body;
 const emoji = incomingRequest.emoji;
 const id = incomingRequest.id;

 fs.readFile("./database.json", "utf8", (err, data) => {
 if (err) {
   console.log(`Error reading file from disk: ${err}`);
 } else {
  const postsData = JSON.parse(data);
  if (emoji === "fa-thumbs-up") {
    postsData[id - 1].likes++;
  }
  if (emoji === "fa-thumbs-down") {
    postsData[id - 1].dislikes++;
  }
  if (emoji === "fa-laugh-squint") {
    postsData[id - 1].laughs++;
  }

  fs.writeFile(
    "./database.json",
    JSON.stringify(postsData, null, 4),
    (err) => {
      if (err) {
        console.log(`Error writing file: ${err}`);
      }
    }
    );
  }
   res.status(201).json([emoji, id]);
  });
 });

  server.post("/comments", (req, res) => {
  const incomingRequest = req.body;
  const comment = incomingRequest.comment;
  const id = incomingRequest.id;

fs.readFile("./database.json", "utf8", (err, data) => {
 if (err) {
   console.log(`Error reading file from disk: ${err}`);
  } else {
  const postsData = JSON.parse(data);
  postsData[id - 1].comments.push(comment);
  fs.writeFile(
    "./database.json",
    JSON.stringify(postsData, null, 4),
    (err) => {
      if (err) {
        console.log(`Error writing file: ${err}`);
        }
      }
     );
   }
   res.status(201).send(comment);
    });
  });

What is causing this error, do I need to write a test which will access the database, im abit confused and how would I go about doing that?

Aucun commentaire:

Enregistrer un commentaire