简体   繁体   English

findOneAndUpdate Mongoose w/ PUT REST API

[英]findOneAndUpdate Mongoose w/ PUT REST API

This is my first attempt at this, it's very simple, I just want to find all documents with provided parameter (box: "Box 1") and of those find all the documents with the provided property (supplies: "Cleaning").这是我第一次尝试,它非常简单,我只想找到所有具有提供参数的文档(框:“框 1”),并找到所有具有提供属性的文档(供应:“清洁”)。 Then update all of those documents from box: "Box 1" to box: "Box 2".然后将所有这些文档从框:“框 1”更新为框:“框 2”。 The result would be all cleaning supplies are now moved to Box 2. So a document would go from {box: "Box 1", supplies: "Cleaning"} to {box: "Box 2", supplies: "Cleaning"}结果是所有清洁用品现在都移到了 Box 2。因此,文件将从 {box: "Box 1", supply: "Cleaning"} 到 {box: "Box 2",supplies: "Cleaning"}

Error错误

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

Schema & Model架构和 Model

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

API 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); }) })

You're missing the preceding '/' before the actual route您在实际路线之前缺少前面的“/”

This line in your API您的 API 中的这一行

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

Should really be真的应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM