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

139
Vistas
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
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