如何从node,js中的表单中获取输入数据



我的node.js应用程序中有一个表单,我想从输入字段中获取数据并通过POST发送。post端点是有效的,我用Postman测试了它,但当我填写表单时,它只是向数据库发送一个空对象,而没有表单字段信息,我该如何实现这一点?这是我的电子病历表:

<h1>Create a new post!</h1>
<form action="/post" method="POST">
<div>
<label for="title">Title</label>
<input type="text" id="title" name="title" required>
</div>
<div>
<label for="content">Post Content</label>
<input type="text" id="content" name="content" required>
</div>
<div>
<label for="categories">Categories</label>
<input type="categories" id="categories" name="categories" required>
</div>
<div>
<label for="date">Date</label>
<input type="text" id="date" name="date" required>
</div>
<div>
<label for="picture">Picture</label>
<input type="text" id="picture" name="picture" required>
</div>
<button type="submit">Create Post!</button>
</form>

和我的服务器.js:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const BASE_URL = process.env.BASE_URL;
const PORT = process.env.PORT || 1337;
// let Post = require('./models/post.model.js');
app.use(cors());
app.use(bodyParser.json());
mongoose.connect(BASE_URL, { useNewUrlParser: true, useUnifiedTopology: true })
const connection = mongoose.connection;
connection.once('open', function () {
console.log('Connection to MongoDB established succesfully!');
});
app.set('view-engine', 'ejs');
app.get('/', async (req, res) => {
res.render('index.ejs');
});
app.get('/', async (req, res) => {
res.render('login.ejs');
});
app.get('/post', async (req, res) => {
res.render('post.ejs');
});
app.post('/post', async (req, res) => {
let collection = connection.collection("posts_with_tags_test");
res.setHeader('Content-Type', 'application/json');
// let post = new Post(req.body);
let post = {
title: req.body.title,
postContent: req.body.content,
categories: req.body.categories,
date: req.body.date,
picture: req.body.picture
}
collection.insertOne(post)
.then(post => {
res.status(200).json({ post })
console.log(post.title)
})
.catch(err => {
res.status(400).send('failed')
});
})
app.listen(PORT);

这是我的模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let Post = new Schema({
title: {
type: String
},
postContent: {
type: String
},
categories: {
type: Array
},
date: {
type: String
},
picture: {
type: String
}
});
module.exports = mongoose.model('Post', Post);

更改此项:

app.use(bodyParser.json());

到此:

app.use(bodyParser.urlencoded({ extended: true}))

HTML表单不将JSON数据作为post请求发送,而是发送应用程序/x-www-form-urlencoded数据,这些数据需要由body解析器解码。

您创建了猫鼬帖子模式吗?如果是的话,你能把它贴在这里吗。您使用mongoose模式来创建一个新的模型,如下例所示:

app.post("/person", async (request, response) => {
try {
var person = new PersonModel(request.body);
var result = await person.save();
response.send(result);
} catch (error) {
response.status(500).send(error);
}
});

如果你的节点服务器正在启动,并且你能够发布到mongo,mongoose/DAL层可能会导致这个问题。

在这里,我认为你可以使用猫鼬模型,看起来你已经评论了:// let post = new Post(req.body);

let post = {
title: req.body.title,
postContent: req.body.content,
categories: req.body.categories,
date: req.body.date,
picture: req.body.picture
}

最新更新