【发布时间】:2021-09-09 10:07:39
【问题描述】:
我目前正在使用 MongoDB (Mongoose) 和 Express 构建应用程序的后端。我正在研究 API,并注意到在测试对 /movies 的 POST 请求时,它没有从 req.body 提交任何信息。当我使用虚拟数据对其进行测试并将信息硬编码到要提交的新电影中时,它可以工作,并且这是唯一发送到数据库的数据。部分代码和示例如下:
这是到movies 的API 路由代码,所有代码都在api.js 文件中找到。
router.get("/movies", (req, res, next) => {
//this will return all the data, exposing only the id and action field to the client
Movie.find({}, "movie")
.then((data) => res.json(data))
.catch(next);
});
router.post("/movies", (req, res, next) => {
let movie = new Movie();
movie.name = req.body.name;
movie.description = req.body.description;
movie.image = req.body.imageURL;
movie.date = req.body.date;
movie.runningTime = req.body.runningTime;
movie.trailer = req.body.trailer;
movie.cast = [];
movie.rating = 0;
movie.reviews = [];
movie.meta = {
likes: 0,
};
console.log(movie);
movie.save((err, movie) => {
if (err) return next(err);
res.status(201);
});
});
下面是架构和模型:
const Schema = mongoose.Schema;
const movieSchema = new Schema({
name: String,
description: String,
image: String,
date: Date,
runningTime: Number,
trailer: String,
cast: [{type: Schema.Types.ObjectId, ref: 'actor'}],
rating: Number,
reviews: [
{
author: String,
title: String,
text: String,
rating: Number,
},
],
meta: {
likes: Number,
},
});
const Movie = mongoose.model("movie", movieSchema);
module.exports = Movie;
movie 在发送一个填满所有数据的 POST 请求后的样子:
{
meta: { likes: 0 },
cast: [],
_id: 60d6e848d952c95f9ccd4dc8,
reviews: [],
rating: 0
}
这是我的index.js 文件:
const express = require("express");
const mongoose = require("mongoose");
const routes = require("./api/api");
const bodyParser = require("body-parser");
require("dotenv").config();
const app = express();
app.use(express.json());
const port = process.env.PORT || 5000;
// Connect to DB
mongoose
.connect(process.env.DB, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connection to DB was made"))
.catch((err) => console.error(err));
mongoose.Promise = global.Promise;
// Header stuff makes configuration easier
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.use("/api", routes);
app.use((err, req, res, next) => {
console.log(err);
next();
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
提交的所有数据都不是来自req.body,而是全部硬编码。我不太清楚为什么没有从 req.body 读取任何内容,因此我们将不胜感激!
【问题讨论】:
-
您可能希望包含(在您的帖子中)正在使用的 HTML 表单标记。另外,如果您遗漏任何内容,请参阅此Express - req.body。
-
在 POST 请求的情况下需要一个 body parser 中间件。
-
目前,您正在使用 express.json() 中间件来解析传入的请求。因此,请求应采用 JSON 格式,否则不会填充 req.body。
标签: node.js mongodb api express mongoose