Express在JavaScript中不使用扩展运算符
我正在编写一个应用程序,您可以通过Axios发送一个表单,然后将其发布到Express控制的表单上。但是在我的app.post()中,我有一个…req.body,结果中没有显示Express在JavaScript中不使用扩展运算符,javascript,html,express,axios,Javascript,Html,Express,Axios,我正在编写一个应用程序,您可以通过Axios发送一个表单,然后将其发布到Express控制的表单上。但是在我的app.post()中,我有一个…req.body,结果中没有显示 function exibirResultado(id, dados){ const texto = JSON.stringify(dados) document.getElementById(id).innerHTML = texto } axio
function exibirResultado(id, dados){
const texto = JSON.stringify(dados)
document.getElementById(id).innerHTML = texto
}
axios.post('formulario', {
nome: 'João',
sobrenome: 'Silva'
}).then(resp => exibirResultado('post', resp))
这是我的客户代码
app.post('/formulario', (req, res) => {
res.send({
...req.body,
id: 5
})
})
在HTML中,有一个带有id post的div,但是在它上面,它只显示form id属性。Req.body应该是一个对象,这一点的证明是它的用法-您可以:
const newComment = new Comment({
body: req.body.body,
author: req.body.author
});
如果我没弄错的话,就不能在上面使用扩展运算符,因为它不是数组。只需在不使用扩展运算符的情况下发送请求正文:
app.post('/formulario', (req, res) => {
res.send({
req.body,
id: 5
})
})
如果需要将其作为数组发送,请将其用一对括号括起来,如下所示:
[req.body]
[...req.body]
const exp = require("express")
const _server = exp()
_server.use(exp.json())
如果要将其添加到现有阵列中,则可以使用如下排列运算符:
[req.body]
[...req.body]
const exp = require("express")
const _server = exp()
_server.use(exp.json())
您正在使用express json中间件吗?否则,express可能在解析正文时遇到问题。试试这样的东西:
[req.body]
[...req.body]
const exp = require("express")
const _server = exp()
_server.use(exp.json())
你犯了什么错误?嘿,伙计,谢谢你的帮助,这对我没用,但我真的很感激你试图帮助我。