gpt4 book ai didi

javascript - 发布时请求对象为空

转载 作者:行者123 更新时间:2023-11-30 19:35:59 26 4
gpt4 key购买 nike

我在 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',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 可以为空,我已经在我的 app.js 中设置了它,所以我真的不确定这里发生了什么。

编辑:我的 express package.json 版本说 ~4.16.0 可能会有所不同,所以如果这意味着它高于或等于 4.16,我不需要使用 bodyParser 吗?

最佳答案

注意:您需要在输入字段中传递名称并发布数据以表达 Node 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')

关于javascript - 发布时请求对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945597/

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