node.js - axios PUT 不更新数据

标签 node.js rest express vue.js axios

我有一个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/

相关文章:

javascript - nodejs .env 变量显示未定义

javascript - NodeJS/Electron : Is it possible to register “STRG+<” as a globalShortcut and how can I do this?

rest - REST 比 GraphQL 更适合的项目?

rest - Microsoft.OData.Core 和 Microsoft.Data.OData 之间的区别

node.js - 连接到 websocket NodeJS Express 服务器时出错

node.js - 将 Express React Redux 模板部署到 Heroku 的步骤

database - 如何通过 Node.js 为 Redis 数据库发出 HGET/GET 命令?

Node.js 调整图像大小

spring - postman - 查看压缩响应大小的选项..?

node.js - 如何更新 Mongoose 中的部分但非全部字段