为什么请求正文向服务器端发送值 undefined 或 null?

Why is request body sending values undefined or null to server side?

提问人:ben holsinger 提问时间:9/26/2021 更新时间:9/26/2021 访问量:317

问:

服务器端代码使用带有 clearDb 插件的 Heroku。

POST 请求正在使用 Postman:请参阅屏幕截图:

请注意,我正在发送一个包含三个字符串值的 JSON 对象。

enter image description here

这是我收到的错误:enter image description here

这是 MYSQL 表和列

enter image description here

这是服务器端用于 POST 和 MYSQL 查询的 js。这可能是一个简单的答案,但是,对此的任何帮助将不胜感激。

app.post('/' , (req,res) => {
    const {email, name, question} = req.body;
    res.header("Access-Control-Allow-Origin", "*");
    console.log(`Your Email is ${email} and your name is ${name} and your ${question}`);



//MYSQL updating table

pool.query("INSERT INTO customer_questions (name, email, question) VALUES (?,?,?)",
    [name, email, question], (err,result)=> {
        if (err) {
            console.log(err)
        }else {
            res.send('data sent')
        }
        
    }
    );


});
 

MySQL Heroku Post Postman 服务器端

评论

1赞 Chris 9/26/2021
一个好的开始可能是将 Content-type 标头设置为 application/json
0赞 ben holsinger 9/26/2021
谢谢,是的,就是这样。只需要将其从文本更改为 JSON 并使用双引号更新键。真的很感谢第二只眼睛。

答:

0赞 ben holsinger 9/26/2021 #1

必须将标头类型从文本更改为 JSON,并使用双引号更新密钥。