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

144
Views
PUT request works in Postman but not in React

I'm building an app with MERN stack that allows to create accounts and then post or delete posts. Before the post shows up on the main page it has to be approved by user with rank of administrator. For now I'm trying to implement a feature where user can approve it's own post. It works perfect in Postman but keeps getting rejected in front end without any error messages.

Here are my backend files:

postRoutes.js

const express = require("express")
const router = express.Router()
const { getPosts, setPost, updatePost, deletePost } = require("../controllers/postController")
const { protect } = require("../middleware/authMiddleware")

router.route("/").get(getPosts).post(protect, setPost)
router.route("/delete/:id").delete(protect, deletePost)
router.route("/update/:id").put(protect, updatePost)

module.exports = router

updatePost function in postController.js

const updatePost = asyncHandler(async (req, res) => {
  const post = await Post.findById(req.params.id)

  if (!post) {
    res.status(400)
    throw new Error("Post not found")
  }

  if (!req.user) {
    res.status(401)
    throw new Error("User not found")
  }

  if (post.user.toString() !== req.user.id && req.user.admin === false) {
    res.status(401)
    throw new Error("User not authorized")
  }

  const updatedPost = await Post.findByIdAndUpdate(req.params.id, req.body, { new: true })

  res.status(200).json(updatedPost)
})

And frontend files:

updatePost function in postService.js

const updatePost = async (postId, data, token) => {
  const config = {
    headers: {
      Authorization: `Bearer ${token}`
    }
  }

  const response = await axios.put(API_URL + `update/${postId}`, data, config)

  return response.data
}

updatePost function in postSlice.js

export const updatePost = createAsyncThunk("posts/update/:id", async (postId, postData, thunkAPI) => {
  try {
    const token = thunkAPI.getState().auth.user.token
    return await postService.updatePost(postId, postData, token)
  } catch (error) {
    const message = (error.response && error.response.data && error.response.data.message) || error.message || error.toString()
    return thunkAPI.rejectWithValue(message)
  }
})

button onClick function in my frontend component

onClick={() => dispatch(updatePost(post._id, { approved: true }))}
about 4 years ago · Juan Pablo Isaza
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!