必需的主体在multer

必需的主体在multer

本文介绍了必需的主体在multer.diskStorage()中不可用.错误“请求"类型上不存在属性主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用Multer将文件保存到本地存储中,而我想做的就是根据req.body中的字段指定文件名.

I am trying to save files to local storage using Multer and what I want to do is specify the file name based on the fields in req.body.

基本上,文件名应类似于contractorId-projctId.但是VS Code显示了以下错误:未在req上定义body属性,并且当我发送文件时,将其另存为undefined-undefined.png.这是我的代码的屏幕截图.我添加了此屏幕截图,以突出显示VS Code尖叫req.body的事实.

Basically, the filename should be something like contractorId-projctId. But VS Code shows the error that body property is not defined on req and when I send the file it saves it as undefined-undefined.png. Here is a screenshot of my code. I've added this screen shot to highlight the fact that VS Code is screaming about req.body.

这是uploadFiles.js的代码

Here is the code for uploadFiles.js

// @ts-check
import express from 'express';
import bodyParser from 'body-parser';
import router from './routes/router';

const app = express();

// Setting up middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '15mb' }));


// Setting up routes
app.use('/', router);

// Error handling
app.use('*', (req, res) => {
  res.status(404).json({
    code: 404,
    error: 'Not found',
    msg: "The resource you're looking for doesn't exist",
  });
});

export default app;

我已经在堆栈溢出中搜索了类似的问题,但大多数问题与文件上传有关.该文件可以很好地上传,并且我已经通过将响应发送回邮递员来测试req.body也很好.这是请求.

I've searched the stack overflow for similar questions but the most question are related to file uploads. The file is being uploaded just fine and I've tested that req.body is also fine, by sending a response back to postman. Here is the request.

这是我在router.js中的代码.

Here is my code in router.js.

// @ts-check
import Router from 'express';
import upload from '../configs/filesUploads';

const router = Router();

router.get('/', (req, res) => {
  res.json({ Okay: true });
});

router.post('/uploads', (req, res) => {
  upload(req, res, (err) => {
    if (err) {
      res.json({
        error: err,
      });
    } else {
      console.log(req.file);
      res.json({ test: 'Meh', body: req.body });
    }
  });
});
export default router;

这是app.js

// @ts-check
import express from 'express';
import bodyParser from 'body-parser';
import router from './routes/router';

const app = express();

// Setting up middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '15mb' }));


// Setting up routes
app.use('/', router);

// Error handling
app.use('*', (req, res) => {
  res.status(404).json({
    code: 404,
    error: 'Not found',
    msg: "The resource you're looking for doesn't exist",
  });
});

export default app;

推荐答案

来自 multer文档 :

尝试通过首先放置projectIdcontractorId并在最后上传文件来重新排列POST正文字段.

Try to rearrange your POST body fields by putting projectId and contractorId first, and the file upload at the end.

这篇关于必需的主体在multer.diskStorage()中不可用.错误“请求"类型上不存在属性主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:15