簡體   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