javascript react js post data to node js server with axios

kh212irz  于 2023-09-29  发布在  Java
关注(0)|答案(3)|浏览(91)

你好,我有一个axios请求将数据发布到node js服务器,但当我试图访问服务器中的此数据时,我得到它未定义。这是函数post data:

function Submit() {

    let params={
        firstName:name,
        lastName:lastName,
        email:email,
        passwordKey:pass,
        statut:1
    }

    axios.post(`http://localhost:8080/new_user`,stringify(params))
    .then((response)=>(console.log(response)))


}

这是node js server中的index中的路由:

app.post('/new_user',(req,res)=>{

    
    const recived=req.body.params
    console.log(recived)
    /*const query = `INSERT INTO user  VALUES()`;
    db.query( query,function (error ,results){
        if(!error ){
            return  res.status(201).json(results)
        }
        else{
                //return   res.status(503).json({"message":"Error in connection"})
                return res.status(500).json(data)
        }
      

    })*/
    
    
})

这是我在服务器上遇到的问题:

TypeError: Cannot read properties of undefined (reading 'params')
    at C:\reactjs\Back-end\index.js:65:28
    at Layer.handle [as handle_request] (C:\reactjs\Back-end\node_modules\express\lib\router\layer.js:95:5)      
    at next (C:\reactjs\Back-end\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\reactjs\Back-end\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\reactjs\Back-end\node_modules\express\lib\router\layer.js:95:5)      
    at C:\reactjs\Back-end\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\reactjs\Back-end\node_modules\express\lib\router\index.js:346:12)
    at next (C:\reactjs\Back-end\node_modules\express\lib\router\index.js:280:10)
    at serveStatic (C:\reactjs\Back-end\node_modules\serve-static\index.js:75:16)
    at Layer.handle [as handle_request] (C:\reactjs\Back-end\node_modules\express\lib\router\layer.js:95:5)
zmeyuzjn

zmeyuzjn1#

当您将数据作为第二个参数传递给post请求处理程序时,axios会自动处理JSON.stringify,这样您就不需要再次执行JSON.stringify()。下面是请求中发生的情况:

const t = JSON.stringify({hello: "World!"}); // results '{"hello":"World!"}'
const j = JSON.stringify(t); // results "{\\"hello\\":\\"World!\\"}"

因此,您可以通过在nodejs后端记录req.body来检查接收到的结果。
下面是如何解决这个问题:

function Submit() {

    let params={
        firstName: name,
        lastName: lastName,
        email: email,
        passwordKey: pass,
        statut: 1,
    };

    axios.post('http://localhost:8080/new_user', params)
    .then((response)=>(console.log(response)));
}

我希望这能解决你的问题。

bvjxkvbb

bvjxkvbb2#

对于这个问题,你可以通过下面的方式在node js中的index中编写代码

if(Object.keys(req.body).length > 0) {
   let firstName = {
             firstName : req.body.firstName,
             lastName : req.body.lastName,
             email:req.body.email,
             passwordKey:req.body.pass,
             status:req.body.status
            }
    }

这会给予你一个准确的结果。

b5buobof

b5buobof3#

我假设您尝试发出JSONcontent-type 请求。所以,您的 axiosPOST请求是错误的。更正如下:

function Submit() {
    // ...
    axios.post("http://localhost:8080/new_user", {
        params,
    })
    .then((response)=>(console.log(response)))
}

相关问题