簡體   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