Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

601
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda