gpt4 book ai didi

node.js - Express.js 应用程序错误 : Cannot read property 'transfer-encoding' of undefined

转载 作者:行者123 更新时间:2023-12-03 08:47:26 28 4
gpt4 key购买 nike

我正在开发 blogging application (单击链接查看 GitHub 存储库) Express , EJS和 MongoDB。

我正在尝试引入添加帖子图像功能。作为 Express 的新手,我对遇到的问题感到困惑。

添加帖子表单:

<form action="/dashboard/post/add" method="POST" enctype="multipart/form-data" class="mb-0">
<div class="form-group">
<input type="text" class="form-control" name="title" value="<%= typeof form!='undefined' ? form.titleholder : '' %>" placeholder="Title" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="excerpt" value="<%= typeof form!='undefined' ? form.excerptholder : '' %>" placeholder="Excerpt" />
</div>
<div class="form-group">
<textarea rows="5" class="form-control" name="body" placeholder="Full text"><%= typeof form!='undefined' ? form.bodyholder : '' %></textarea>
</div>
<label for="postimage">Upload an image</label>
<div class="form-group">
<input type="file" name="postimage" id="postimage" size="20">
</div>
<div class="form-group d-flex mb-0">
<div class="w-50 pr-1">
<input type="submit" value="Add Post" class="btn btn-block btn-md btn-success">
</div>
<div class="w-50 pl-1">
<a href="/dashboard" class="btn btn-block btn-md btn-success">Cancel</a>
</div>
</div>
</form>

在 Controller 中,我的 addPost() 方法如下所示:

const Post = require('../../models/post');
const { validationResult } = require('express-validator');

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/images')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + '.png')
}
});

const upload = multer({ storage: storage }).single('postimage');

exports.addPost = (req, res, next) => {

upload(function(err) {
if (err) {
console.log("There was an error uploading the image.");
}
res.json({
success: true,
message: 'Image uploaded!'
});
})

var form = {
titleholder: req.body.title,
excerptholder: req.body.excerpt,
bodyholder: req.body.body
};

const errors = validationResult(req);

const post = new Post();

post.title = req.body.title;
post.short_description = req.body.excerpt;
post.full_text = req.body.body;

if (!errors.isEmpty()) {
req.flash('danger', errors.array())
res.render('admin/addpost', {
layout: 'admin/layout',
website_name: 'MEAN Blog',
page_heading: 'Dashboard',
page_subheading: 'Add New Post',
form: form
});
} else {
post.save(function(err) {
if (err) {
console.log(err);
return;
} else {
req.flash('success', "The post was successfully added");
req.session.save(() => res.redirect('/dashboard'));
}
});
}
}

我还在( Controller 的)顶部有 const multer = require("multer");

“添加新帖子”表单工作正常,直到我尝试添加此上传功能。我当前的代码抛出此错误:

Cannot read property 'transfer-encoding' of undefined
at hasbody (C:\Path\To\Application\node_modules\type-is\index.js:93:21)

我做错了什么?

最佳答案

您的 upload() 中缺少 reqres,请尝试添加这两个,例如

upload(req, res, function (err) {
if (err) {
console.log("There was an error uploading the image.");
}
res.json({
success: true,
message: 'Image uploaded!'
});
})

关于node.js - Express.js 应用程序错误 : Cannot read property 'transfer-encoding' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849398/

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