繁体   English   中英

将 res.send 值从 node.js 后端传递给 react.js

[英]Passing res.send value from node.js backend to react.js

我正在尝试使用 res.send 从 nodejs 后端传递一个字符串

app.post("/user", (req,res) => {
console.log(req.body.email);
res.send('haha');
});

我想在前端根据检索到的字符串值执行一些操作。

axios({ 
        method: 'post', 
        url: '/user', 
        data: { email: this.state.emailFetch } 
        }).then(response => {
            if(response.send == "haha")
            {
                return <Redirect to="/"/> 
            }
        });

我想我搞砸了前端的响应处理。 有人可以指导我吗?

试试response.datares.send()只是发送响应的函数。 您还可以控制台记录整个响应以检查响应值的存储位置。

你的 axios 代码应该是这样的:

axios({ 
    method: 'post', 
    url: '/user', 
    data: { email: this.state.emailFetch } 
    }).then(response => {
        if(response.data == "haha")
        {
            return <Redirect to="/"/> 
        }
    });

你应该阅读 axios 文档。 如果你正在尝试创建一个 API,你应该在大多数情况下使用res.json([])

response.send 是一种方法,因此您不能在前端使用它来从中提取数据。 查看前端发生的事情的最简单方法是 console.log(response) ,您可以查看响应的结构以及数据包含的位置。

暂无
暂无

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

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