javascript - 请求正文未传递给 API
问题描述
我正在尝试从反应表单(Mongoose API)向更新控制器发出 PUT 请求。一切都传递给请求,除了正文。现在,这是我第一次使用 FormData,所以我几乎可以肯定这就是问题所在,但我似乎无法找出问题所在..
表单中的提交操作
const clickSubmit = () => {
// console.log('Values on submit before FormData: ', values) // Shows the state object as expected
let userData = new FormData()
values.name && userData.append('name', values.name)
values.email && userData.append('email', values.email)
values.password && userData.append('password', values.password)
values.about && userData.append('about', values.about)
values.photo && userData.append('photo', values.photo)
update({
userId: match.params.userId
}, {
t: jwt.token
}, userData).then((data) => {
if (data && data.error) {
setValues({...values, error: data.error})
} else {
setValues({...values, 'redirectToProfile': true})
}
})
}
设置请求的辅助方法
const update = async (params, credentials, user) => {
console.log('The params: ', params) // passes the user ID just fine
console.log('The credentials:', credentials) // passes the JWT just fine
console.log('The user object: ', ...user) // has all the information I'm updating, albeit in an array form that I can't really work with
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: user
})
return await response.json()
} catch (err) {
console.log(err)
}
}
而控制器我已经注释掉了其余的逻辑来消除混乱,而我 TS 这个问题
const update = async (req, res) => {
console.log(req)
const user = await User.findById(req.params.userId)
console.log('user after find: ', user) // returns the user that I want to modify from the database
console.log('body of request: ', req.body) // empty object
}
更新:我能够使用 FormData 将 FormData 转换为实际对象Object.fromEntries(user)
- 但它仍然不会传递到请求中。我尝试了两种方法:
const update = async (params, credentials, user) => {
console.log('The params: ', params)
console.log('The credentials:', credentials)
console.log('The user object: ', ...user)
let infoToUpdate = Object.fromEntries(user)
console.log('infoToUpdate: ', infoToUpdate);
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: {
"name": infoToUpdate.name,
"email": infoToUpdate.email,
"about": infoToUpdate.about
}
})
return await response.json()
} catch (err) {
console.log(err)
}
}
和
const update = async (params, credentials, user) => {
console.log('The params: ', params)
console.log('The credentials:', credentials)
console.log('The user object: ', ...user)
let infoToUpdate = Object.fromEntries(user)
console.log('infoToUpdate: ', infoToUpdate);
try {
let response = await fetch('/api/users/' + params.userId, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + credentials.t
},
body: infoToUpdate
})
return await response.json()
} catch (err) {
console.log(err)
}
}
但是 req.body 仍然是一个空对象..
解决方案
这已经解决了,这都是我的翻盖电脑的错..
一时兴起,我杀死了 node_modules 和 package.lock 文件并重新安装了deps ..它开始工作了..我的猜测是bodyParser没有完全安装..
谢谢大家的帮助。
推荐阅读
- dart - 列中 RaisedButton 小部件之间出现不需要的空间
- ios - 为另一个节点使用灯光后保持原始节点照明
- excel - 从excel电子表格的另一行中的数据中查找另一行中的数据
- python - python列表中重复项的最后索引
- c# - 如何在C#中提取文本文件每一行的第一个单词
- c++ - 在函数定义中有条件地执行计算的最佳方式
- php - 带负数的日期时间格式问题
- php - 使用 PHP 连接活动目录或 LDAP
- r - ggplot2 - 如何将带有箭头图标的geom_text分配给第二个y轴刻度
- php - 使用 blackfire 的基于 Docker 的 php 应用程序分析