Code Monkey home page Code Monkey logo

Comments (13)

arb avatar arb commented on May 20, 2024 3

Ok so if req.body is an empty object, then multer isn't parsing the multipart form before req is passing through the celebrate middleware. You probably have to move it down past extractFile. What I would do is keep moving the logging statement down until req.body has some values, and then put the validation middleware at that spot.

from celebrate.

leoyoshii avatar leoyoshii commented on May 20, 2024 3

Ok so if req.body is an empty object, then multer isn't parsing the multipart form before req is passing through the celebrate middleware. You probably have to move it down past extractFile. What I would do is keep moving the logging statement down until req.body has some values, and then put the validation middleware at that spot.

This is worked for me, Thanks 👍.

postRouter.post(
  '/',
  upload.array('photos'),
  celebrate({
    [Segments.BODY]: {
      text: Joi.string().required(),
      title: Joi.string().required(),
    },
  }),
  postController.create,
);

from celebrate.

arb avatar arb commented on May 20, 2024

What multipart form middleware are you using for express? How does it expose multipart form data to your other functions? Also, it's sort of atypical to send data back to the server like this when you could just send JSON rather than the overhead of FormData.

from celebrate.

whisher avatar whisher commented on May 20, 2024

Hi Arb,
thanks for the reply.
I know using FormData can be overload
but I'm working on a form with a image to upload
so the only way to accomplish the task is using FormData.
I'm using multer as multipart form middleware.

from celebrate.

arb avatar arb commented on May 20, 2024

Before celebrate(validators.create), add another middleware and log out req.body to see what's in there before the validation occurs.

from celebrate.

whisher avatar whisher commented on May 20, 2024
module.exports = (req, res, next) => {
  console.log(req.body);
  next();
};
router.post('',
  testBoby,
  celebrate(validators.create),
  jwtVerify,
  extractFile,
  controllers.create
);

I can see

{}

an empty object

Just to clarify without validation
I can do insert tha data in the db without any problem.
BTW I'm using multer as middleware like:

'use strict';
const multer = require('multer');

const MIME_TYPE_MAP = {
  'image/png': 'png',
  'image/jpeg': 'jpg',
  'image/jpg': 'jpg'
};

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    let error = null;
    const isValid = MIME_TYPE_MAP[file.mimetype];
    if (!isValid) {
      error = new Error('Invalid mime type');
    }
    cb(error, 'images');
  },
  filename: (req, file, cb) => {
    const name = file.originalname
      .toLowerCase()
      .split(' ')
      .join('-');
    const ext = MIME_TYPE_MAP[file.mimetype];
    cb(null, name + '-' + Date.now() + '.' + ext);
  }
});

module.exports = multer({ storage: storage }).single('image');

Just to try and debug I tried to set celebrate after extractFile

router.post('',
  jwtVerify,
  extractFile,
 celebrate(validators.create),
  controllers.create
);

with the same result and errors

from celebrate.

arb avatar arb commented on May 20, 2024

Put a log statement like this

router.post('',
  (req, res, next) => { console.log(req.body); next(); }
  celebrate(validators.create),
  jwtVerify,
  extractFile,
  controllers.create
);

If it's an empty object, then you have an issue before this route. You've posted a lot of disjointed code so it's hard to see exactly what your server is doing.

from celebrate.

whisher avatar whisher commented on May 20, 2024

this is my testBody is the same
of your example :)

module.exports = (req, res, next) => {
  console.log(req.body);
  next();
};

and it logs an empty object.

an issue before this route

There is no issue before this route
but as you know express doesn't handle
form data without any middleware like multer

from celebrate.

whisher avatar whisher commented on May 20, 2024

I've just tried
#79 (comment)
(at the end)
but it doesn't work :(

from celebrate.

arb avatar arb commented on May 20, 2024

Like I said, keep moving the logging statement around until req.body is not empty. Looking at the documentation to multer this isn't a problem with celebrate and you're just having issues with putting it in the right position. celebrate only validations against req.body, if you have it in a position where req.body is an empty object, the validation is going to fail.

Also looking (again) at the multer docs, I see this note:

Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.

So it may be impossible to do this the way you have it written.

Additionally, you can check through the multer issues related to req.body, it seems you aren't the only person with issues of req.body being empty.

from celebrate.

whisher avatar whisher commented on May 20, 2024

Ok, thanks for the help :)

from celebrate.

arb avatar arb commented on May 20, 2024

Were you ever able to figure out this issue?

from celebrate.

whisher avatar whisher commented on May 20, 2024

No, I wasn't :(

from celebrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.