简体   繁体   English

Express.js 应用程序错误:无法读取未定义的属性(读取“传输编码”

[英]Express.js application error: Cannot read properties of undefined (reading 'transfer-encoding'

I am working on a blogging app API with express and MongoDB.我正在使用 express 和 MongoDB 开发博客应用程序 API。

I am trying to add a post image for every blog post.我正在尝试为每篇博客文章添加一个帖子图片。 Being new to express, I ran into this problem.作为新手,我遇到了这个问题。

And i got this error: Cannot read properties of undefined (reading 'transfer-encoding'我得到了这个错误:无法读取未定义的属性(读取“传输编码”

Here is the post controller code code:这里是post controller code代码:

const asyncHandler = require("express-async-handler");

const imageModel = require("../models/imageModel");

const User = require("../models/userModel");
const Post = require("../models/postModel");

//  storage
const Storage = multer.diskStorage({
  destination: "storage",
  filename: (res, req, file, cb) => {
    cb(null, Date.now() + file.originalname);
  },
});

const upload = multer({
  storage: Storage,
}).single("img");

// @desc    Create a new post
// @route   POST /api/posts/
// @access  Private
const createPost = asyncHandler(async (res, req) => {
  let img;

  upload(req, res, async function (err) {
    if (err) {
      res.status(400);
      throw new Error("Error uploading images.");
    }
    const newImage = await new imageModel({
      img: {
        data: req.file.filename,
        contentType: "image/png",
      },
    });
    newImage.save().then(console.log("Successfully uploaded"));
    img = newImage;
  });

  console.log(img);

  const { title, description, categories, nacCompatible, downloadURL } =
    req.body;

  if (!title || !description || !categories || !downloadURL) {
    res.status(400);
    throw new Error("Please add all the required fields.");
  }

  // Get user using the id in the JWT
  const user = await User.findById(req.user.id);

  if (!user) {
    res.status(401);
    throw new Error("User not found");
  }

  const post = await Post.create({
    title,
    description,
    img,
    categories,
    nacCompatible,
    downloadURL,
    user: req.user._id,
    status: "new",
  });

  res.status(201).json(post);
});

I also have const multer = require("multer");我也有const multer = require("multer"); at the top of the controller.在 controller 的顶部。

The create post function worked fine until I tried to add this upload image feature.在我尝试添加此上传图片功能之前,创建帖子 function 工作正常。

This line: "createPost = asyncHandler(async (res, req) "这一行:“createPost = asyncHandler(async (res, req)

You put the req and res in the wrong order你把reqres放在错误的顺序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 expess.js TypeError 中发布请求错误:无法读取未定义的属性“fullName” - Post request error in expess.js TypeError: Cannot read property 'fullName' of undefined 类型错误:无法使用 express-jwt 读取未定义的属性“授权” - TypeError: Cannot read property 'authorization' of undefined using express-jwt node.js/express.js 连接池无法读取未定义的属性(读取“getConnection”) - node.js/express.js connection pool Cannot read properties of undefined (reading 'getConnection') Node.js无法读取未定义的属性“ firstname” - Node.js Cannot read property 'firstname' of undefined express.js 中缺少“/”字符 HTTP.get() 请求返回 TypeError:无法读取未定义的属性(读取“0”) - lack of "/" character in express.js HTTP .get() request returning TypeError: Cannot read properties of undefined (reading '0') 无法读取未定义的属性“body” - Cannot read property 'body' of undefined TypeError:无法读取未定义的属性“位置” - TypeError: Cannot read property 'location' of undefined 无法读取未定义的属性(读取“传输编码”) - Cannot read properties of undefined (reading 'transfer-encoding') Sequelize,Express JS - 无法读取未定义的属性(读取“findAll”) - Sequelize, Express JS - Cannot read properties of undefined (reading 'findAll') NodeJs类TypeError:无法读取未定义的属性“名称” - NodeJs Classes TypeError: Cannot read property 'name' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM