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

185
Views
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 answers
Answer question

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 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!