简体   繁体   English

将“req.body.data”解构为“req.body”

[英]Destructuring 'req.body.data' into 'req.body'

I am sending some data from the front end to the back end.我正在从前端向后端发送一些数据。 I am trying to send data as an object to the back.我正在尝试将data作为 object 发送到后面。 When it arrives, it is obviously req.body.data , but should land in the backend as req.body .当它到达时,它显然是req.body.data ,但应该作为req.body后端。 How do I destructure the request, either from the front or the back so that the back only receives req.body如何从前面或后面解构请求,以便后面只接收req.body

front end前端

const { user } = await axios.post('http://localhost:3000/auth/signup', {data});

Backend (what I want)后端(我想要的)

if(req.body.type === 'vendor') {

Right now the backend received (What i dont want)现在后端收到了(我不想要的)

if(req.body.data.type....)

Your passing data as an object prop.您作为 object 道具传递的数据。 Just pass it directly:直接传过去就好了:

const { user } = await axios.post('http://localhost:3000/auth/signup', data)

You are sending object with data property when sending request to the backend.向后端发送请求时,您正在发送带有data属性的 object。 What you should do is destruct that data object and send its property directly:您应该做的是破坏该data object 并直接发送其属性:

const { user } = await axios.post('http://localhost:3000/auth/signup', { ...data });

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

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