简体   繁体   中英

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"). Then update all of those documents from box: "Box 1" to box: "Box 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"}

Error

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

Schema & Model

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

You're missing the preceding '/' before the actual route

This line in your API

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

Should really be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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