繁体   English   中英

出现错误无法在我的 REST API 中实现“PUT”方法 REST http 请求

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

我正在尝试使用猫鼬方法创建一个 REST API,因为当我尝试在特定文章部分中实现 PUT 模型时。 当我从 Postman 发送 PUT 请求时

然后我在控制台中收到以下错误:

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)

错误截图

Postman界面截图

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

在运行代码并从数据库连接它,并从邮递员发送 http 请求后,我只看到了这个错误,但看不到我的数据库中的任何更改。

您需要使用正文解析才能使用req.body ,否则它将是未定义的。

添加以下行

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

const app = express();之后

暂无
暂无

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

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