When I console.log
the req.body
it gives me:
[Object: null prototype] {
'shoe[name]': 'Shoe Name',
'shoe[description]': '',
'shoe[pictures]': '',
'shoe[collections]': 'Shoe Collection'
}
But when I console.log
req.body.shoe
It prints undefined
,
been breaking my head for a few days now
The form:
<form action="/shoes/" method="post">
<input type="text" name="shoe[name]" placeholder="Name of the shoe">
<textarea name="shoe[description]" cols="30" rows="10"></textarea>
<input type="file" name="shoe[pictures]">
<input type="text" name="shoe[collections]">
<input type="submit" value="Post the Shoe">
</form>
Shoe Model:
const mongoose = require('mongoose');
const shoeSchema = new mongoose.Schema({
name: { type: String, unique: true },
description: String,
displayPicture: mongoose.Schema.Types.ObjectId,
pictures: [ { type: mongoose.Schema.Types.ObjectId } ],
collections: [ { type: mongoose.Schema.Types.ObjectId, unique: true } ],
dateOfPublish: { type: Date, default: Date.now },
comments: [
{
userId: { type: mongoose.Schema.Types.ObjectId, unique: true, ref: 'Comment' }
}
]
});
module.exports = mongoose.model('Shoe', shoeSchema);
Node.js + Express create route:
//Create
router.post('/', (req, res) => {
console.log(req.body.shoe);
Shoe.create(req.body.shoe, (err, shoe) => {
if (err) {
console.log(err);
} else {
return res.redirect('/');
}
});
});