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

186
Visualizações
Is my route set up correct to update json server in react

I am trying to update some information in my json server but I still don't seem to reach my end points. Am I setting mu urls correct. I have tried a couple times changing my url. In my mind I thinking I am doing the correct thing. But error messages telling me other wise. I would appreciate any guidance. Thanks in advance.

Component functions

  const [author, setAuthor] = useState("");
  const [text, setText] = useState("");

  useEffect(() => {
    const loadArticleInfo = async () => {
      try {
        const response = await axios.get(`http://localhost:5000/articles/${id}`)
        setArticleInfo(response.data)
      } catch (err) {
        console.log(err);
      }
    };
    loadArticleInfo();
  }, [id]);

  const addUpvote = async () => {
    const response = await axios.put(`/articles/${id}/upvotes`, {...articleInfo, upvotes: articleInfo.upvotes + 1, });
    const updatedArticle = response.data;
    setArticleInfo(updatedArticle);
  };

  const addComment = async () => {
    const temp_comments = [...articleInfo.comments, {author: author, text: text}];
    console.log(temp_comments, "New Comment");
    const response = await axios.put(`http://localhost:5000/articles/${id}/comments`, {...articleInfo, comments: temp_comments});
    console.log(response.data)
     setArticleInfo({...articleInfo, comments: response.data});
    setAuthor("");
    setText("");

Sample JSON

{
  "articles": [
    {
      "id": "2",
      "name": "learn-node",
      "title": "How to Build a Node Server in 10 Minutes",
      "content": [
        "`In this article, ....."
      ],
      "upvotes": 9,
      "comments": [
        {
          "author": "anon",
          "text": "Node really changed things for me since I no longer have to master PHP... JS all the way!"
        },
        {
          "author": "Tester",
          "text": "No!!! Not another server!"
        }
   
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Edited my put urls to this

const response = await axios.put(/articles/${id}/, {...articleInfo, upvotes: articleInfo.upvotes + 1, }

about 4 years ago · Santiago Gelvez 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