Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

605
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda