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

124
Views
Reactjs request empty in nodejs

When sending ReactJS request using post method, the req. body object in NodeJS has no values while I tested the api using postman and it worked as expected. I'm using axios to connect ReactJS to the API and express in nodejs ReactJS code:

function CreatePost(props) {
    let [file, setFile] = useState();
    let navigate = useNavigate();
    const params = useParams();
    let groupeId = params.id;
    console.log(groupeId)

    const validationSchema = yup.object({
        content:  yup.string().min(10, 'Must be more than 10 characters').required('description is required')
    })
    const onSubmit = async () => {
        let item = new FormData()
        item.append('title', formik.values.title)
        item.append('content', formik.values.content)
        item.append('file', file)

        let user = JSON.parse(localStorage.getItem('userInfo'))

        let res = await api.post(`/createPoste/${user.id}/${groupeId}/`, item)
        if (res.data === false)
            console.log('creation de post impossible')
        else
            navigate(`/groupe/home/${groupeId}`)
    }
    const formik = useFormik({
        initialValues: {
            title: '',
            content: ''
        },
        onSubmit,
        validationSchema
    })

Nodejs code:

const express = require('express')
const bodyParser = require('body-parser')

const router = express.Router()

const db = require('../db')
router.use(bodyParser.urlencoded({extended: true}));

router.post('/createPoste/:userId/:groupeId', async (req, res) => {
    console.log(req.body)
    let post = {
        userId: req.params.userId,
        groupeId: req.params.groupeId,
        title: req.body.title,
        content: req.body.content
    }
    await db.query("insert into postes set ?", post, function (err, result){
        if (err) {
            console.log(err.message)
            res.send(false)
        }
        else res.send(true)
    })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In axios you should send the data in body. Append the userID and groupeId into the item formData().

about 4 years ago · Juan Pablo Isaza 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!