javascript - req.body.content 给出 undefined - Express JS

标签 javascript node.js rest express

我是 Node 和 express 的新手。我尝试创建笔记应用程序的示例 API。

当我尝试通过使用 POSTMAN 创建新笔记来测试 API 时,我得到未定义的 req.body.title 和 req.body.content 的值。但是当我试图控制 req.body 时,我得到以下信息:

 { '{"title": "Chemistry Note", "content": "Lorem ipsum note clasds"}': '' }

我正在使用最新版本的 express、bodyParser、mongoose。

这是 note.controller.js 文件

exports.create = (req, res) => {

    console.log(req.body.title);
    // Validating request
    if(!req.body.content) {
        return res.status(400).send({
            message: "Note content cannot be empty"
        });
    }

    // Creating a Note
    const note = new Note({
        title: req.body.title || "Untitled Note",
        content: req.body.content
    });

    // Saving Note
    note.save()
        .then(data => {
            res.send(data);
        }).catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while creating the Note."
            });
        });
};

这是 server.js 文件:

const express = require("express");
const bodyParser = require("body-parser");

// Creating Express Application
const app = express();

// Parse request of content-type - application/x-www-form-url-encoded
app.use(bodyParser.urlencoded({extended: true}));

// Parse request of content type - application/json
app.use(bodyParser.json());

const dbConfig = require("./config/database.config");
const mongoose = require("mongoose");

// Using native promises
mongoose.Promise = global.Promise;

// Connecting to Database
mongoose.connect(dbConfig.url, {
    useNewUrlParser: true
}).then(() => {
    console.log("Successfully connected to the database");
}).catch(err => {
    console.log("Could not connect to the database. Exiting now...", err);
    process.exit();
});

// Define a simple route
app.get('/', (req, res) => {
    res.json({"message": "Welcome to Note Application"});
});

// Require Notes routes
require("./app/routes/note.routes.js")(app);

// Listen for requests
app.listen(3000, () => {
    console.log("Listening on port 3000");
});

下面是 note.model.js 文件:

const mongoose = require("mongoose");

// Defining Schema
const NoteSchema = mongoose.Schema({
    title: String,
    content: String
}, {
    timestamps: true
});

// Creating Model with this schema
module.exports = mongoose.model('Note', NoteSchema);

下面是 note.routes.js 文件:

module.exports = (app) => {

    // Contains the methods for handling CRUD operations
    const notes = require("../controllers/note.controller.js");

    // Creating new Note
    app.post('/notes', notes.create);
};

请帮忙,谢谢。任何帮助将不胜感激。

最佳答案

{ '{"title": "Chemistry Note", "content": "Lorem ipsum note clasds"}': '' }, 
it's not a object you want. 
For this json your key is '{"title": "Chemistry Note", "content": "Lorem ipsum note clasds"}'. 
And its don't have any key like title, content.

Change above object to
{"title": "Chemistry Note", "content": "Lorem ipsum note clasds"}

关于javascript - req.body.content 给出 undefined - Express JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094901/

相关文章:

node.js - Passport 抛出 user.authenticate 不是一个函数

c# - HttpContext.Current 类 Node 中的对象

子资源的API设计?

java - 并发: Designing a REST API using Play Framework/Akka

javascript - 如何拖放某些东西但它不会消失所以我们可以继续拖放它

javascript - 拦截来自网页的每个请求

javascript - 为什么在声明的对象上解构 ES6 会出现问题?

javascript - 如何重命名时刻时区中的 precisionDiff 键?

node.js - 为什么yarn install 会 check out github 存储库依赖项,而npm install 却不会?

php - Python RESTful 客户端,例如来自 PHP 的 Guzzle