1

when I send a request using a form, the body object does not contain the email value required..

for example when I use postman:

enter image description here

this error is a Schema error, because the email is not coming through....

I have tried to parse the headers using bodyparser, but no luck..

but when I send it using raw JSON it works fine:

enter image description here

my root server route:

const express = require('express');
const app = express();
const mongoose = require('mongoose');


var cors = require('cors');

app.use(cors());

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
});


// parse body on every request //
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.use('/api/post', require('./routes/postRoute'));
app.use('/api/users', require('./routes/usersRoute'));


// root home page
app.get('/', (req, res) => {
    res.send('hi, love you ok? bye');
});

// connect to DB //

mongoose
    .connect('mongodb://localhost:27017/Express_Sessions_Wireframe', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true
    })
    .then(console.log('We have touchdown houston [DATABASE HAD CONNECTION]'))
    .catch((err) => console.log(err));

// listen to server
app.listen(3000, () => {
    console.log('roger roger [server listening]');
});

my user Schema :

let User = new Schema(
    {
        email: {
            type: String,
            required: true,
            unique: true
        },
        posts: [
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Post'
            }
        ]
    },
    {
        timestamps: true
    }
);

module.exports = mongoose.model('User', User);

my post route

const express = require('express');
const User = require('../models/userModel');


var router = express.Router();

// parse json to use in all requests got or sent by router .. which is provided by express
router.use(express.json());


// User can signUp for an account
router.post('/signup', (req, res, next) => {
                User.create({
                    email: req.body.email
                })
        .then(
            (user) => {
                res.statusCode = 200;
                console.log( 'Registration Successful!', `user ====== ${user}` )
                res.setHeader('Content-Type', 'application/json');
                res.json({ status: 'Registration Successful!', user: user });
            },
            (error) => next(error)
        )
        .catch((err) => next(err));
});



module.exports = router;

1 Answers1

1

mutlipart/form-data is not handled by bodyparser library, as noted here: https://www.npmjs.com/package/body-parser#readme

There are other libraries that can help with parsing multipart/form-data. They are noted in the link above. Here is an excerpt...

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

busboy and connect-busboy

multiparty and connect-multiparty

formidable

multer

jfarleyx
  • 775
  • 1
  • 5
  • 9
  • that can't be the issue, I also tried app.use(express.json()) , no luck either.. –  Jan 02 '20 at 22:47
  • @jcx3x But, the way you handle multipart form data in express is using a 3rd party llbrary, such as multer. Without it, you won't get the email address when it's sent as multipart form data. Using app.use(express.json()) isn't going to work for form data. You can see an example of how to use Multer here: https://stackoverflow.com/questions/37630419/how-to-handle-formdata-from-express-4 – jfarleyx Jan 02 '20 at 22:57
  • H'm, yes I think you are right... but my formdata is still returning an empty object unfortantly... even with multer. –  Jan 02 '20 at 23:04
  • @jcx3x Can you edit your question and include your code using multer? I'd be happy to take a look and see if I can spot the issue. – jfarleyx Jan 02 '20 at 23:05
  • nevermind, I ignored the second field because in the example they would not allow files and my app allows files, but doing this now.. the data is coming, THANK YOU SO MUCH –  Jan 02 '20 at 23:06
  • 1
    @jcx3x That's great! Happy to help! Happy New Year. – jfarleyx Jan 02 '20 at 23:08