如何将id和body添加到axios.PUT请求?

我正在用REACT使用axios,并想知道如何使用它来通过我设置的后端节点/ express API来更新对象的put请求。

我需要能够通过idbody ,不知道如何这样做。

 axios.put('/api/profile', id, body) .then(response => { console.log(response.data); }) .catch(err => { console.log(err); }); 

我不认为这将工作,因为它需要put(url, data, {headers})

正常的模式是让你想要更新/放在前端,然后使用该id作为axios请求的id:

 axios.put(`/api/profile/${id}`, body) //using string interpolation axios.put('/api/profile' + id, body) //using string concatenation 

然后,在你的express / node后端,你有一个路由,将匹配这个请求的URIpath,并更新该configuration文件的正文。 不知道你用什么数据库的东西,但在伪代码它看起来像这样:

 router.put( '/api/profile/:id', ( req, res, next ) => { Profile.findbyId( req.params.id ) //query profile Model for the specific profile .then( foundProfile => { //make sure to check if foundProfile is undefined or not. foundProfile.update( req.body ) ) //update profile with body .catch( next ) //catch on errors }) 

正如在上面的评论中提到的,你可以通过查询string来做到这一点,但这将是奇怪的/反模式。