Reputation: 6388
I'm trying to create a CRUD with nodejs with express and mongoose, when i try to update the content from the form, this empty all the registers when i do it with POST method. Here comes my code:
Jade template
extends ../includes/layout
block content
div(class="wrap")
include ../includes/header
div(class="column-group vertical-space")
div(class="all-50 push-center")
div(class="all-100")
h1 #{title}
div(class="all-100")
form(role="form" method="post" action="#{project.id}" enctype="multipart/form-data")
div(class="all-50")
h3 Cambiar nombre a #{project.ProjectName}
input(type="text" name="projectName" required)
p Cambiar detalles
textarea(name="details")
p Cambiar precio
input(type="number" name="ammount")
p Cambiar localización
input(type="text" name="localize")
div(class="all-50")
p Cambiar Imágen
input(type="file" name="image" multiple)
//p #{plane}
//input(type="file" name="plane")
div(class="all-100")
button(type="submit") Enviar
div(class="push")
include ../includes/footer
controller
saveEditProject : function (req, res, file){
Project.findById(req.params.id, function(err, project){
if(!project){
res.redirect('/project');
}
project.ProjectName = req.body.projectName;
project.ProjectDetails = req.body.details;
project.ProjectAmount = req.body.ammount;
project.ProjectLocation = req.body.localize;
project.ProjectFileName = req.body.image;
project.save(function (err) {
if (err) {
res.send("not now");
}
res.redirect('/project');
});
});
},
route
router.post('/edit/:id', controller.saveEditProject);
Upvotes: 1
Views: 2455
Reputation: 21
You need to use PUT method instead of POST. For using PUT you need to first install the method override module, which can be done as... npm install method-override --save.
Upvotes: 2
Reputation: 695
BodyParser isn't meant for image uploading so make sure you have for example multer in your server.js:
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
Then just follow their page and for example do this:
router.post('/edit/:id', upload.array(), controller.saveEditProject);
The key here is to use a parser that deals with images. You have in your Jade form the following attribute:
enctype="multipart/form-data"
That is what will "disable" BodyParser because it will not interpret it. If you take it out you will see BodyParser working.
PS: Sorry for not giving you a more detailed answer right now, this is what I can give you at the moment :) Their page is very good though and I'm sure you will find the solution very easily!
Happy coding!
Upvotes: 1