findOneAndUpdate Mongoose w / PUT REST API

这是我的第一次尝试,这很简单,我只是想find所有提供参数的文件(方框:“方框1”),并find所有提供的属性(供应:“清洁”)的文件。 然后从框中更新所有这些文件:“框1”到框:“框2”。 结果就是所有的清洁用品现在都被转移到方框2.所以文档将从{方框:“方框1”,耗材:“清洁”}到{方框:“方框2”,耗材:“清洁”}。

错误

"Cannot PUT /updatebox/Box%201/Cleaning" //in Postman "404 Not Found" //in Postman log 

架构和模型

 var boxSchema = new mongoose.Schema({ box: String, supplies: String }); var boxModel = mongoose.model('boxModel', boxSchema, 'boxList'); 

API

 app.put('updatebox/:box/:supplies', function (req, res) { var boxReq = req.params.box; var suppliesReq = req.params.supplies; boxModel .find({box: boxReq}) .findOneAndUpdate({supplies: suppliesReq}, { $set: { box: "Box 2" } }, {new: true}, function (err, doc) { if (err) { console.log("Something wrong when updating data!"); } res.json(doc); }); }) 

在实际路线之前,您错过了前面的“/”

这行在你的API

app.put('updatebox/:box/:supplies', function (req, res) {

应该是真的

app.put('/updatebox/:box/:supplies', function (req, res) {