Express.js POST req.body empty

后端 未结 4 656
北海茫月
北海茫月 2020-12-06 18:34

So I have the following code in my server.js file that I\'m running with node.js. I\'m using express to handle HTTP requests.

app.post(\'/api/destinations\',         


        
相关标签:
4条回答
  • 2020-12-06 18:48

    You need bodyParser.json as well:

    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    
    0 讨论(0)
  • 2020-12-06 18:55

    Sometimes the req.body shows {} if you forgot to put the name attribute to the form input fields. Following is an example:

    <input type="email" name="myemail" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
    

    Then the req.body shows { myemail: 'mathewjohnxxxx@gmail.com' }

    I post this answer because, i have encountered the similar problem and this worked for me.

    0 讨论(0)
  • 2020-12-06 19:00

    For me the back end was configured properly the issue was valid JSON format including double quotes on the variables.

    this did not work

          const res = await axios.post(serverPath + "/user/login", {
             email: email,
             password: password,
          });
    

    This DID work (with double quotes around email and password

          const res = await axios.post(serverPath + "/user/login", {
             "email": email,
             "password": password,
          });
    
    0 讨论(0)
  • 2020-12-06 19:02

    Make sure if you are making the form and inputs in javascript that you append the input to the form. That was my issue.

    yourForm.appendChild(yourInput);
    
    0 讨论(0)
提交回复
热议问题