繁体   English   中英

request.body 回来了 undefined

[英]request.body is coming back undefined

所以我将用户输入的数据传递给后端,但 req.body 返回未定义,我不知道为什么。 我已经预先设置了我的中间件,但我仍然没有定义。 我还检查了 axios 发送的值,并检查出是正确的。

app.use(Cors());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}));

app.post('/signup', (req, res) => {
    //const { email } = req.body;
    console.log(req.body.email);
    if(!req.body.email) {
        console.log("No email, failed");
        return;
    }
    const data = {
        members: [
            {
                email_address: req.body.email,
                status: "subscribed"
            }
        ]
    }

    const postData = JSON.stringify(data);
    const options = {
        url: 'https://us4.api.mailchimp.com/3.0/lists/xxxxxx',
        method: 'POST',
        headers: {
            Authorization: 'xxxxxxx'
        },
        body : postData
    }

    request(options, (err, response, body) => {
            if(err) {
                console.log(err);
            } else {
                if(response.statusCode === 200) {

                    console.log("Success!");
                }else {

                    console.log("Error Accessing")
                    res.send("Error");
                }
            }       
        });
});

这是我的 axios post 方法:

handleSubmit = e => {
        e.preventDefault();
        const { email } = this.state;
        axios
        .post('http://localhost:5000/signup', email)
        .then(() => console.log("Email Added"))
        .catch(err => {
            console.log(err);
        })
    };

您已经确认 axios (this.state.email) 发布的内容不是未定义的?

req 主体返回 undefined 的事实表明数据输入不正确或对该数据的请求无效。

整个 req.body 是什么样的?

我的问题是我没有将电子邮件作为对象传递,因此该对象返回空。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM