Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

599
Views
Svelte (Express & Axios) - http 500 error on put method

I'm at a loss. I have a svelte application (Backend: Express, Frontend: Axios). I have a MongoDB with locations. Locations have an array of bands. And I want to add bands to this array. The backend seems to work fine, at least with Postman it works. But when I try to add a band through the frontend, I get a http 500 error.

This is the back end code:

app.put('/api/locations/:location', async (req, res) => {

let location = req.params.location;
let updatedlocation = req.body;


try {
    await client.connect();
    const database = client.db('Concerts');
    const collection = database.collection('locations');
    const query = { locationname: location };
    const result = await collection.updateOne(query, { $set: updatedlocation });

    if (result.matchedCount === 0) {
        let responseBody = {
            status: "No Location under the name " + location
        }
        res.status(404).send(responseBody);
    }
    else {
        res.send({ status: "Location " + location + " has been updated." });
    }
} catch (error) {
    res.status(500).send({ error: error.message });
}})

This is the method in the frontend:

function addConcert() {
    location.concerts.push(chosenband);
    console.log("http://localhost:3001/api/locations/" +name);
    console.log(location);
    axios.put("http://localhost:3001/api/locations/" +name, location)
        .then((response) => {
            alert("Konzert wurde hinzugefügt");
        })
        .catch((error) => {
            alert("Nope");
            console.log(error);
        });
}  

Info: the {chosenband} comes from a select. This seems to work as well, as the console logs show. The object is correct and includes the new band:

this is the log from the browser

So the object seems fine. Also the put-url is correct.

But I always get this 500 error

Thankful for any advise!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Found the problem. I didn't instantiate the location correctly. First I instantiated it simply as

let location = {}

, that didn't work. When I instantiated it with all the attributes

let location = {example:"", second:""}

it worked. Thanks for your help

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!