gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:18:48 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com