skip to Main Content
    const SignUpForm = () => {
   const {values ,errors , handleChange , handleSubmit} = useFormik({
    initialValues ,
    validationSchema: SignupSchema,
    onSubmit : async ()=>{
        const user = await  fetch('http://localhost:8080/user/register' ,{
       method : "POST" ,
       body : JSON.stringify(values)
     })
     console.log(user ,values);
    }
})



   
 const router = express.Router()
router.use(bodyParser.urlencoded({extended : false}))
router.use(express.json())

router.post('/register' , async(req,res)=>{
        console.log(req.body);
        res.json({
                message : 'success'
        })
})
      

I am trying to get form values in server , but it’s returning me an empty object. is there any issue with post request? . I have tried everything I know but its not working .

2

Answers


  1. please do the below change and check

    Write below code:

    app.use(express.json())
    

    Instead of

    router.use(express.json())
    
    Login or Signup to reply.
  2. @KASHi Try below code :

    const express = require('express');
    const app = express();
    const PORT = 3000;
    app.use(express.json())    // <==== parse request body as JSON
    
    // Single routing
    const router = express.Router();
    
    router.get('/', function (req, res, next) {
        console.log("Router Working");
        res.end();
    })
    router.post('/', function (req, res, next) {
        console.log(" POST Router Working",req.body);
        res.end();
    })
    
    app.use(router);
    
    app.listen(PORT, function (err) {
        if (err) console.log(err);
        console.log("Server listening on PORT", PORT);
    });
    

    I tried above It’s working. compare with your code, if still not works then paste your whole server js file here so I can help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search