我在哈巴狗中设置了一个表单,如下所示:
extends layout
block content
h1 David A Hines
h2 #{posts[0].title}
p #{posts[0].body}
div
form(action='/insert_post',method='post')
div(id='title_div',data-role='fieldcontain')
fieldset(data-role='controlgroup')
p
label(for='title') Title
p
input(id='title',type='text',value='',placeholder='')
fieldset(data-role='controlgroup')
p
label(for='body') Body
p
textarea(id='body',cols="40", rows="5", type='textarea',value='',placeholder='')
fieldset(data-role='controlgroup')
label(for='author_name') Author Name
input(id='author_name',type='text',value='',placeholder='')
label(for='author_email') Author Email
input(id='author_email',type='text',value='',placeholder='')
div(data-role='fieldcontain')
input(type='submit',value='Submit Post',data-transition='fade', data-theme='c')
它命中/ insert_post,其中/ insert post在数据库中记录,但是请求对象完全为空。
这是我的insert_post操作:
router.post('/insert_post', function(req, res, next) {
var postsArray = [];
MongoClient.connect(database.url, function(err, db) {
var db = db.db(database.database);
if (!err) {
console.log("We are connected");
var postsCollection = db.collection('posts');
console.log("request: "+JSON.stringify(req.body));
var postObject = {
title: req.body.title,
body: req.body.body,
author: {
name: req.body.name,
email: req.body.email
}
}
postsCollection.insertOne(postObject, function(err, response) {
console.log("Inserted Post: "+JSON.stringify(response.ops[0]));
res.render('index', {
posts: response.ops[0]
});
});
} else {
console.log("error:" + err);
res.end("error: " + err);
}
});
});
我知道,如果您没有安装body-parser,则request.body可以为null,我已在我的app.js中对其进行了设置,因此我真的不确定这是怎么回事。
编辑:我的express的package.json版本表示〜4.16.0可能会有所不同,所以如果这意味着它大于或等于4.16,我就不需要使用bodyParser吗?
答案 0 :(得分:1)
注意:您需要在输入字段中传递名称并发布数据以表示节点js。
服务器端代码:-
router.post('/insert_post', function(req, res, next) {
var postsArray = [];
MongoClient.connect(database.url, function(err, db) {
var db = db.db(database.database);
if (!err) {
console.log("We are connected");
var postsCollection = db.collection('posts');
console.log("request: "+JSON.stringify(req.body));
var postObject = {
title: req.body.title,
body: req.body.body,
author: {
name: req.body.author_name,
email: req.body.author_email
}
}
postsCollection.insertOne(postObject, function(err, response) {
console.log("Inserted Post: "+JSON.stringify(response.ops[0]));
res.render('index', {
posts: response.ops[0]
});
});
} else {
console.log("error:" + err);
res.end("error: " + err);
}
});
});
pug文件代码:-
extends layout
block content
h1 David A Hines
h2 #{posts[0].title}
p #{posts[0].body}
div
form(action='/insert_post',method='post')
div(id='title_div',data-role='fieldcontain')
fieldset(data-role='controlgroup')
p
label(for='title') Title
p
input(id='title',name='title',type='text',value='',placeholder='')
fieldset(data-role='controlgroup')
p
label(for='body') Body
p
textarea(id='body',name='body',cols="40", rows="5", type='textarea',value='',placeholder='')
fieldset(data-role='controlgroup')
label(for='author_name') Author Name
input(id='author_name',name='author_name',type='text',value='',placeholder='')
label(for='author_email') Author Email
input(id='author_email',name='author_email',type='text',value='',placeholder='')
div(data-role='fieldcontain')
input(type='submit',value='Submit Post',data-transition='fade', data-theme='c')