Express.js应用程序错误:无法读取未定义的属性“传输编码” [英] Express.js application bug: Cannot read property 'transfer-encoding' of undefined

查看:55
本文介绍了Express.js应用程序错误:无法读取未定义的属性“传输编码”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 博客应用程序 (点击指向请参见 GitHub 回购)和 Express EJS 和MongoDB。

I am working on a blogging application (click the link to see the GitHub repo) with Express, EJS and MongoDB.

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

I am trying to introduce an add post image feature. Being quite new to Express, I am puzzled about the problem I have ran into.

添加帖子的形式:

<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>

在控制器中,我的 addPost()方法看起来像这样:

In the controller my addPost() methos looks like this:

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'));
            }
        });
    }
}

我也有 const multer = require( multer); 位于顶部(控制器的顶部)。

I alse have const multer = require("multer"); at the top (of the controller).

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

The "Add New Post" form worked fine until I tried to add this upload feature. The code I currently have throws this error:

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

我在做什么错了?

推荐答案

您缺少 req res 放在您的 upload()中,请尝试添加这两个像 upload( req,res ,函数(err){

You are missing req and res inside your upload(), try adding those two like upload(req,res, function (err){

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

这篇关于Express.js应用程序错误:无法读取未定义的属性“传输编码”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆