我正在编写一个包含react、mondodb、mongoose和nodejs的全栈项目。
我的路线功能
const handleBlockUser = () => {
if(isCheck.length) {
isCheck.map(async (userId) => {
console.log(userId)
const blockUser = await request(`/api/auth/`, 'PUT', {userId})
})
}
}
请求函数
const request = useCallback(async (url, method = 'GET', body = null, headers = {}) => {
setLoading(true)
我的路由
router.put('/', async (req, res) => {
const {id} = req.body
User.find(id)
.then(doc=>{
doc.status = tableState;
return doc.save();
})
.then(doc=>res.json(doc))
.catch(err=>console.log(err))
})
在按钮中我传递了正确的ID,但在路由的控制台中它是未定义的。为什么?
1条答案
按热度按时间4ioopgfo1#
因为你还没有在PUT请求中传递
userId
。你已经把null
设置为主体。在这里传递userId
。我不确定你在使用什么库来发送请求。如果你使用axios,这篇文章会有所帮助。https://stackabuse.com/how-to-make-put-http-request-with-axios/