【问题标题】:I cant update other records than first in database via axios&postman我无法通过 axios postman 更新数据库中的第一个记录以外的其他记录
【发布时间】:2020-05-23 12:48:37
【问题描述】:

由于某些未知原因,即使我可以毫无问题地获取所有记录,我也只能更新数据库中的第一条记录。这是它的外观: 更清楚地说:名称是唯一的,这就是我使用它们而不是 id 的原因,我在邮递员中更改 url 以更新一些记录,但我只能更新第一个记录。它与前端相同,当我点击其他“PAPP”时,它返回给我有效的 url 但无效的数据..

代码:

router.put("/:name", (req, res, next) => {
  Papp.findOneAndUpdate({ _pappName: req.params.pappName }, req.body, {
    new: true
  }).then(function(state) {
    res.send(state);
  });
});

架构代码:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const pappSchema = new Schema({
  pappName: {
    type: String,
    required: true
  },
  maintanceName: {
    type: String,
    required: true
  },
  maintanceIsOn: {
    type: Boolean,
    required: true
  },
  autosearchName: {
    type: String,
    required: true
  },
  autosearchValue: {
    type: String,
    required: true
  },
  commentName: {
    type: String,
    required: true
  },
  commentValue: {
    type: String,
    required: true
  }
});

module.exports = Papp = mongoose.model("Papp", pappSchema);

【问题讨论】:

  • 把你的findOneAndUpdate函数也放上去。粘贴代码,而不是截图
  • 如您所愿编辑!

标签: javascript reactjs express axios


【解决方案1】:

您的请求参数的名称是name,但您在代码中使用了req.params.pappName。所以将req.params.pappName 替换为req.params.name。 在过滤器查询中也使用pappName 而不是_pappName

router.put("/:name", (req, res, next) => {
  Papp.findOneAndUpdate({ pappName: req.params.name }, req.body, {
    new: true
  }).then(function(state) {
    res.send(state);
  });
});

【讨论】:

  • 如果我像你说的那样返回我的空数据字符串:/
  • @Goteii 您可以将您的 Papp 架构代码添加到问题中吗?
  • 好的,请稍等
猜你喜欢
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多