简体   繁体   中英

Getting Error not able to impliment 'PUT' method REST http request in my REST API

I am trying to create a REST API using mongoose methods, in that when I am trying to implement PUT model in specific article section. And when I am sending the PUT request from Postman

Then I am getting the following error in my console:

TypeError: Cannot read properties of undefined (reading 'title')
    at E:\REST api\Wiki-API\app.js:83:31
    at Layer.handle [as handle_request] (E:\REST api\Wiki-API\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\REST api\Wiki-API\node_modules\express\lib\router\route.js:144:13)
    at next (E:\REST api\Wiki-API\node_modules\express\lib\router\route.js:138:14)
    at Route.dispatch (E:\REST api\Wiki-API\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\REST api\Wiki-API\node_modules\express\lib\router\layer.js:95:5)
    at E:\REST api\Wiki-API\node_modules\express\lib\router\index.js:284:15    at param (E:\REST api\Wiki-API\node_modules\express\lib\router\index.js:365:14)
    at param (E:\REST api\Wiki-API\node_modules\express\lib\router\index.js:376:14)
    at Function.process_params (E:\REST api\Wiki-API\node_modules\express\lib\router\index.js:421:3)

Error Screenshot

Screenshot of Postman interface

 //jshint esversion:6 const express = require("express"); const bodyParser = require("body-parser"); const ejs = require("ejs"); const mongoose = require('mongoose'); const app = express(); app.set('view engine', 'ejs'); app.use(express.static("public")); mongoose.connect("mongodb://localhost:27017/wikiDB", { useNewUrlParser: true }); const articleSchema = { title: String, content: String }; const Article = mongoose.model("Article", articleSchema); //*************** Request targeting all articles *************** app.route("/articles") .get(function (req, res) { Article.find({}, function (err, foundArticles) { if (!err) { res.send(foundArticles); } else { res.send(err); } }); }) .post(function (req, res) { const newArticle = new Article({ title: req.body.title, content: req.body.content }); newArticle.save(function (err) { if (!err) { res.send("Successfully added the article."); } else { res.send(err); } }); }) .delete(function (req, res) { Article.deleteMany( {}, function (err) { if (!err) { res.send("Successfully deleted all article."); } else { res.send(err); } } ) }); //*************** Request targeting specific articles *************** app.route("/articles/:articleTitle") .get(function (req, res) { Article.findOne({ title: req.params.articleTitle }, function (err, foundArticle) { if (foundArticle) { res.send(foundArticle); } else { res.send("No article matching that title was found."); } }); }) .put(function (req, res) { Article.updateOne( { title: req.params.articleTitle }, { title: req.body.title, content: req.body.content }, { overwrite: true }, function (err, res) { if (!err) { res.send("successfully updated article!"); } else { res.send(err); } } ); }); app.listen(3000, function () { console.log("Server started on port 3000"); });

After running the code and connecting it from the database, and sending http request from the postman I just seeing this error but not able to see any changes in my database.

You need to use the body parse in order to use the req.body , otherwise it will be undefined.

Add the following line

app.use(express.json());
app.use(express.urlencoded());

after const app = express();

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