-3

Whenever I try to update an object from the genres array using the code below, nothing happens. I get a 200 status code but the object remains the same with no updated value. where could I be going wrong? I'll greatly appreciate all the help I can get.

const schema = Joi.object({
    name: Joi.string().min(3).required()
});

app.put('/api/genres/:id', (req, res) => {
    const genre = genres.find(g => g.id === parseInt(req.params.id))
    if(!genre) return res.status(404).send("Not Found")
    res.send(genre)

    const {error, value} = schema.validate(req.body)
      
    if (error) {
        console.log(error);
        return res.status(400).send("Bad Request");
    };
    
    genre.name = req.body.name;
    res.send(genre)
})
New contributor
thecodingalchemist is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • 2
    Looks like you are validating your data, then you change a property of your object in memory - but I don't see any step where you actually persist the data in permanent storage? Based on the examples shown under medium.com/@artemkhrenov/…, I'd expect there to be something like await genre.save(); before you return anything ...? Commented yesterday
  • This code will always cause an error ("Headers already sent"). Have you been ignoring that error? Commented yesterday
  • Thanks! I really appreciate your responses Commented 15 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.