我有一个VueJS
应用程序,它使用Axios
将一个对象PUT
到服务器。目标是更新数据库中的该对象。该对象已存在于数据库中,但使用 PUT
发回的对象有一些已更改的数据。这些更改就是我想要更新到数据库的内容。应用程序运行时不会出现错误消息,但数据库中的数据永远不会更新。
Client side
onUpload(): void {
this.chosenRoute.name = this.routeName;
this.chosenRoute.text.paragraphs[0] = this.routeDescription;
this.chosenRoute.text.preamble = this.preamble;
if(this.activity != "") this.chosenRoute.activity = this.activity;
axios.put('http://localhost:8080/route/' + this.selectedRoute, JSON.stringify(this.chosenRoute), {
onUploadProgress: uploadEvent => {
console.log('Upload Progress' + Math.round(uploadEvent.loaded / uploadEvent.total) * 100 + " %");
}
}).then(res => {
console.log(res);
});
}
Server side
app.put('/route/:id', function(req, res, next) {
const routeId = req.params.id;
res.set("Access-Control-Allow-Origin", "*");
Route.update({'_id':routeId}, req.body, function(result) {
return res.send(result);
});
});
最佳答案
我认为你不应该使用 JSON.stringify
所以你的代码应该如下所示:
axios.put(`http://localhost:8080/route/${this.selectedRoute}`, this.chosenRoute, {
onUploadProgress: uploadEvent => {
console.log('Upload Progress' + Math.round(uploadEvent.loaded / uploadEvent.total) * 100 + " %");
}
}).then(res => {
console.log(res);
});
希望对您有帮助!
关于node.js - axios PUT 不更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438529/