【发布时间】: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