postman 获取错误无法在我的REST API中实现'PUT'方法REST http请求

ckx4rj1h  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(167)

我尝试使用mongoose方法创建一个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)

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

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

bwntbbo3

bwntbbo31#

您需要使用主体解析才能使用req.body,否则它将是未定义的。
添加以下行

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

const app = express();之后

相关问题