--- tags: debugging, practical, solutions --- # Debugging Practical - Solutions These are the solutions for the Debugging Practical https://github.com/CommandShiftHQ/be-debugging-practical There may be more than one way of correcting the errors, below is one possible way to fix the code. # Table of Contents [TOC] ## Debug Task 1 Solution In `app.js`, path wrong and missing ! from end of Hello World ```js app.get("/hello", (_, res) => { res.status(200).json("Hello World!"); }); ``` ___ ## Debug Task 2 Solution In `app.js`, the http method used is wrong. It should be `.delete` instead of `.get` ```js app.delete("/goodbye", (_, res) => { res.sendStatus(204); }); ``` ___ ## Debug Task 3 Solution(s) ### 3(a) Solution In `app.js`, the status code is wrong, it should be `201`. ### 3(b) Solution In `app.js`, you need to use `req.body.pokemon` insteaad of hard coding the response ```js app.post("/pokemon", (req, res) => { res.status(201).json(req.body.pokemon); }); ``` ___ ## Debug Task 4 Solution In `app.js`, can’t use `.json` after using `.sendStatus` ```js app.get("/pokemon", (_, res) => { res.status(200).json("Catch them all!"); }); ``` ___ ## Debug Task 5 Solution In `app.js`, you need to *send* a response not just set a status ```js app.delete("/pokemon", (_, res) => { res.sendStatus(204); }); ``` ___ ## Debug Task 6 Solution In `app.js`, age is being sent in the request body, not as a query parameter. ```js app.patch("/greeting/:forename/:surname", (req, res) => { const { forename, surname } = req.params; const { age } = req.body res.status(200).json(`Happy ${age}th birthday, ${forename} ${surname}!`); }); ``` ___ ## Debug Task 7 solution In `app.js`, the pokemon name is passed a query param, not as route param. ```js app.get("/pokemon/choose", (req, res) => { const { name } = req.query; res.status(200).json(`${name}, I choose you!`); }); ``` ## Debug Task 8 solution In `app.js`, `/` missed off path ```js app.get("/albums", (_, res) => { res.status(200).json("Albums"); }); ``` ___ ## Debug Task 9 Solution In `app.js`, need to add `/:username` to path as parameter ```js app.get("/login/:username", (req, res) => { const { username } = req.params; res.status(200).json(`Welcome ${username}`); }); ``` In test file, `body` is not defined so add it to the curly brackets after status. Add a name after the `/login` in the test and change the `.to.equal("Welcome ")` to include this name ```js it("debug 8", async () => { const { status, body } = await request(app).get("/login/harry"); expect(status).to.equal(200); expect(body).to.equal("Welcome harry"); }); ``` ___ ## Debug Task 10 Solution In test file, `.toEqual` should be `.to.equal` and `.toBe` should be `.to.be.an` ```js it("debug 9", async () => { const { status, body } = await request(app).get("/users"); expect(status).to.equal(200); expect(body).to.be.an("array"); }); ``` --- ## Debug Task 11 solution In test file use `.to.have.length` instead of `.to.have.length.greaterThan` ```js it("debug 10", async () => { const { status, body } = await request(app).get("/books"); expect(status).to.equal(201); expect(body).to.be.an("array"); expect(body).to.have.length(3); expect(body[0]).to.have.property("id"); expect(body[0]).to.have.property("title"); expect(body[0]).to.have.property("author"); }); ```