Upload with Multer 2 images, then when edit images only update the first record
uploading 2 images with req.files[0].filename
, req.files[1].filename,
i can edit and update the first record only with req.files[0].filename
, req.files[1].filename,
NOTE i can upload and save images then read it from mongodb but when i want to update it, it's only update the first 2 images with this position
req.files[0].filename
, and req.files[1].filename
Here is my code:
1. uploading 2 images and everything goes alright
router.post('/upload', upload.any(), function(req, res) {
if (!req.body && !req.files) {
req.flash('error', 'error');
res.redirect('admin/home');
} else {
var c;
Photo.findOne({}, function(err, data) {
if (data) {
c = data.unique_id + 1;
} else {
c = 1;
}
var oto = new Photo({
unique_id: c,
smallcaption: req.body.caption,
smallpath: req.files[0].filename,
bigyear: req.body.year,
bigtitle: req.body.title,
bigcategory: req.body.category,
bigpath: req.files[1].filename,
});
oto.save((err, Person) => {
if (err) return err;
console.log(req.files);
req.flash('success_msg', 'Your Project Has Been Saved !!!');
res.redirect('admin/home');
});
})
.sort({ _id: -1 })
.limit(1);
}
});
****2. editing (Updating) my 2 images but only first record is updated****
router.post('/edit-project/(:id)', upload.any(), function(req, res, next) {
var otos = {
smallcaption: req.sanitize('smallcaption').trim(),
smallpath: req.files[0].filename,
bigyear: req.sanitize('bigyear').trim(),
bigtitle: req.sanitize('bigtitle').trim(),
bigcategory: req.sanitize('bigcategory').trim(),
bigpath: req.files[1].filename,
};
var o_id = new ObjectId(req.params.id);
db.collection('photos').update({ _id: o_id }, otos, function(err, result) {
if (err) {
req.flash('error_msg', 'Error Occurred When You Updating Your Data !!!');
res.render('admin/edit-project', {
smallcaption: req.body.smallcaption,
smallpath: req.files[0].filename,
bigyear: req.body.bigyear,
bigtitle: req.body.bigtitle,
bigcategory: req.body.bigcategory,
bigpath: req.files[1].filename,
});
} else {
console.log(req.files);
req.flash('success_msg', 'Data updated successfully !!!');
res.redirect('/admin/home');
}
});
});
3. uploading form
<div id="newproject" class="tabcontent">
<div class="col-lg-6 col-md-6 col-sm-10 col-xs-10">
<form id="proj-form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="s-caption" class="form-control" type="text" name="caption" placeholder="Small Photo Caption">
<input type="file" name="smallimg" id="smallimageUpload" class="hide"/>
<label for="smallimageUpload" class="btn btn-primary">Choose A Small Photo</label>
<img src="" id="smallimagePreview" alt="" class="imageview"/>
<input id="p-year" class="form-control" type="text" name="year" placeholder="Project Year">
<input id="p-title" class="form-control" type="text" name="title" placeholder="Project Title">
<input id="p-Cate" class="form-control" type="text" name="category" placeholder="Project Category">
<input type="file" name="bigimg" id="bigimageUpload" class="hide"/>
<label for="bigimageUpload" class="btn btn-primary">Choose A Big Photo</label>
<img src="" id="bigimagePreview" alt="" class="imageview"/>
<button type="submit" class="btn btn-info">Add Project</button>
<input type="button" class="btn btn-basic" onclick="resetProject()" value="Reset">
</div>
</form>
</div>
<div class="col-lg-3 col-md-3 col-sm-1 col-xs-1"></div>
</div>
4. editing Form
<% for(var i=0; i<fotos.length; i++) {%>
<form method="POST" action="/edit-project/<%= fotos[i]._id %>" enctype="multipart/form-data">
<tr>
<td><%= i+1 %></td>
<td><input type="text" class="caption-wi" name="smallcaption" value="<%= fotos[i].smallcaption %>"></td>
<td><div class="thumbnail">
<input type="file" name="smallimg" id="simgUpload" class="hide"/>
<label for="simgUpload" class="btn btn-primary">Small Photo</label>
</div></td>
<td><input type="text" class="year-wi" name="bigyear" value="<%= fotos[i].bigyear %>"></td>
<td><input type="text" class="update-wi" name="bigtitle" value="<%= fotos[i].bigtitle %>"></td>
<td><input type="text" class="update-wi" name="bigcategory" value="<%= fotos[i].bigcategory %>"></td>
<td><div class="thumbnail">
<input type="file" name="bigimg" id="bimgUpload" class="hide"/>
<label for="bimgUpload" class="btn btn-primary">Big Photo</label>
</div></td>
<td class="order-value">
<input class="" type="text" value="<%= i+1 %>">
</td>
<td>
<input type="submit" class="btn btn-info save ico" value="" name="edit" onClick="return confirm('Save For Sure!?')" />
<input type="hidden" name="_method" value="POST"/>
</td>
</tr>
</form>
<% } %>
javascript node.js express ejs multer
add a comment |
uploading 2 images with req.files[0].filename
, req.files[1].filename,
i can edit and update the first record only with req.files[0].filename
, req.files[1].filename,
NOTE i can upload and save images then read it from mongodb but when i want to update it, it's only update the first 2 images with this position
req.files[0].filename
, and req.files[1].filename
Here is my code:
1. uploading 2 images and everything goes alright
router.post('/upload', upload.any(), function(req, res) {
if (!req.body && !req.files) {
req.flash('error', 'error');
res.redirect('admin/home');
} else {
var c;
Photo.findOne({}, function(err, data) {
if (data) {
c = data.unique_id + 1;
} else {
c = 1;
}
var oto = new Photo({
unique_id: c,
smallcaption: req.body.caption,
smallpath: req.files[0].filename,
bigyear: req.body.year,
bigtitle: req.body.title,
bigcategory: req.body.category,
bigpath: req.files[1].filename,
});
oto.save((err, Person) => {
if (err) return err;
console.log(req.files);
req.flash('success_msg', 'Your Project Has Been Saved !!!');
res.redirect('admin/home');
});
})
.sort({ _id: -1 })
.limit(1);
}
});
****2. editing (Updating) my 2 images but only first record is updated****
router.post('/edit-project/(:id)', upload.any(), function(req, res, next) {
var otos = {
smallcaption: req.sanitize('smallcaption').trim(),
smallpath: req.files[0].filename,
bigyear: req.sanitize('bigyear').trim(),
bigtitle: req.sanitize('bigtitle').trim(),
bigcategory: req.sanitize('bigcategory').trim(),
bigpath: req.files[1].filename,
};
var o_id = new ObjectId(req.params.id);
db.collection('photos').update({ _id: o_id }, otos, function(err, result) {
if (err) {
req.flash('error_msg', 'Error Occurred When You Updating Your Data !!!');
res.render('admin/edit-project', {
smallcaption: req.body.smallcaption,
smallpath: req.files[0].filename,
bigyear: req.body.bigyear,
bigtitle: req.body.bigtitle,
bigcategory: req.body.bigcategory,
bigpath: req.files[1].filename,
});
} else {
console.log(req.files);
req.flash('success_msg', 'Data updated successfully !!!');
res.redirect('/admin/home');
}
});
});
3. uploading form
<div id="newproject" class="tabcontent">
<div class="col-lg-6 col-md-6 col-sm-10 col-xs-10">
<form id="proj-form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="s-caption" class="form-control" type="text" name="caption" placeholder="Small Photo Caption">
<input type="file" name="smallimg" id="smallimageUpload" class="hide"/>
<label for="smallimageUpload" class="btn btn-primary">Choose A Small Photo</label>
<img src="" id="smallimagePreview" alt="" class="imageview"/>
<input id="p-year" class="form-control" type="text" name="year" placeholder="Project Year">
<input id="p-title" class="form-control" type="text" name="title" placeholder="Project Title">
<input id="p-Cate" class="form-control" type="text" name="category" placeholder="Project Category">
<input type="file" name="bigimg" id="bigimageUpload" class="hide"/>
<label for="bigimageUpload" class="btn btn-primary">Choose A Big Photo</label>
<img src="" id="bigimagePreview" alt="" class="imageview"/>
<button type="submit" class="btn btn-info">Add Project</button>
<input type="button" class="btn btn-basic" onclick="resetProject()" value="Reset">
</div>
</form>
</div>
<div class="col-lg-3 col-md-3 col-sm-1 col-xs-1"></div>
</div>
4. editing Form
<% for(var i=0; i<fotos.length; i++) {%>
<form method="POST" action="/edit-project/<%= fotos[i]._id %>" enctype="multipart/form-data">
<tr>
<td><%= i+1 %></td>
<td><input type="text" class="caption-wi" name="smallcaption" value="<%= fotos[i].smallcaption %>"></td>
<td><div class="thumbnail">
<input type="file" name="smallimg" id="simgUpload" class="hide"/>
<label for="simgUpload" class="btn btn-primary">Small Photo</label>
</div></td>
<td><input type="text" class="year-wi" name="bigyear" value="<%= fotos[i].bigyear %>"></td>
<td><input type="text" class="update-wi" name="bigtitle" value="<%= fotos[i].bigtitle %>"></td>
<td><input type="text" class="update-wi" name="bigcategory" value="<%= fotos[i].bigcategory %>"></td>
<td><div class="thumbnail">
<input type="file" name="bigimg" id="bimgUpload" class="hide"/>
<label for="bimgUpload" class="btn btn-primary">Big Photo</label>
</div></td>
<td class="order-value">
<input class="" type="text" value="<%= i+1 %>">
</td>
<td>
<input type="submit" class="btn btn-info save ico" value="" name="edit" onClick="return confirm('Save For Sure!?')" />
<input type="hidden" name="_method" value="POST"/>
</td>
</tr>
</form>
<% } %>
javascript node.js express ejs multer
add a comment |
uploading 2 images with req.files[0].filename
, req.files[1].filename,
i can edit and update the first record only with req.files[0].filename
, req.files[1].filename,
NOTE i can upload and save images then read it from mongodb but when i want to update it, it's only update the first 2 images with this position
req.files[0].filename
, and req.files[1].filename
Here is my code:
1. uploading 2 images and everything goes alright
router.post('/upload', upload.any(), function(req, res) {
if (!req.body && !req.files) {
req.flash('error', 'error');
res.redirect('admin/home');
} else {
var c;
Photo.findOne({}, function(err, data) {
if (data) {
c = data.unique_id + 1;
} else {
c = 1;
}
var oto = new Photo({
unique_id: c,
smallcaption: req.body.caption,
smallpath: req.files[0].filename,
bigyear: req.body.year,
bigtitle: req.body.title,
bigcategory: req.body.category,
bigpath: req.files[1].filename,
});
oto.save((err, Person) => {
if (err) return err;
console.log(req.files);
req.flash('success_msg', 'Your Project Has Been Saved !!!');
res.redirect('admin/home');
});
})
.sort({ _id: -1 })
.limit(1);
}
});
****2. editing (Updating) my 2 images but only first record is updated****
router.post('/edit-project/(:id)', upload.any(), function(req, res, next) {
var otos = {
smallcaption: req.sanitize('smallcaption').trim(),
smallpath: req.files[0].filename,
bigyear: req.sanitize('bigyear').trim(),
bigtitle: req.sanitize('bigtitle').trim(),
bigcategory: req.sanitize('bigcategory').trim(),
bigpath: req.files[1].filename,
};
var o_id = new ObjectId(req.params.id);
db.collection('photos').update({ _id: o_id }, otos, function(err, result) {
if (err) {
req.flash('error_msg', 'Error Occurred When You Updating Your Data !!!');
res.render('admin/edit-project', {
smallcaption: req.body.smallcaption,
smallpath: req.files[0].filename,
bigyear: req.body.bigyear,
bigtitle: req.body.bigtitle,
bigcategory: req.body.bigcategory,
bigpath: req.files[1].filename,
});
} else {
console.log(req.files);
req.flash('success_msg', 'Data updated successfully !!!');
res.redirect('/admin/home');
}
});
});
3. uploading form
<div id="newproject" class="tabcontent">
<div class="col-lg-6 col-md-6 col-sm-10 col-xs-10">
<form id="proj-form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="s-caption" class="form-control" type="text" name="caption" placeholder="Small Photo Caption">
<input type="file" name="smallimg" id="smallimageUpload" class="hide"/>
<label for="smallimageUpload" class="btn btn-primary">Choose A Small Photo</label>
<img src="" id="smallimagePreview" alt="" class="imageview"/>
<input id="p-year" class="form-control" type="text" name="year" placeholder="Project Year">
<input id="p-title" class="form-control" type="text" name="title" placeholder="Project Title">
<input id="p-Cate" class="form-control" type="text" name="category" placeholder="Project Category">
<input type="file" name="bigimg" id="bigimageUpload" class="hide"/>
<label for="bigimageUpload" class="btn btn-primary">Choose A Big Photo</label>
<img src="" id="bigimagePreview" alt="" class="imageview"/>
<button type="submit" class="btn btn-info">Add Project</button>
<input type="button" class="btn btn-basic" onclick="resetProject()" value="Reset">
</div>
</form>
</div>
<div class="col-lg-3 col-md-3 col-sm-1 col-xs-1"></div>
</div>
4. editing Form
<% for(var i=0; i<fotos.length; i++) {%>
<form method="POST" action="/edit-project/<%= fotos[i]._id %>" enctype="multipart/form-data">
<tr>
<td><%= i+1 %></td>
<td><input type="text" class="caption-wi" name="smallcaption" value="<%= fotos[i].smallcaption %>"></td>
<td><div class="thumbnail">
<input type="file" name="smallimg" id="simgUpload" class="hide"/>
<label for="simgUpload" class="btn btn-primary">Small Photo</label>
</div></td>
<td><input type="text" class="year-wi" name="bigyear" value="<%= fotos[i].bigyear %>"></td>
<td><input type="text" class="update-wi" name="bigtitle" value="<%= fotos[i].bigtitle %>"></td>
<td><input type="text" class="update-wi" name="bigcategory" value="<%= fotos[i].bigcategory %>"></td>
<td><div class="thumbnail">
<input type="file" name="bigimg" id="bimgUpload" class="hide"/>
<label for="bimgUpload" class="btn btn-primary">Big Photo</label>
</div></td>
<td class="order-value">
<input class="" type="text" value="<%= i+1 %>">
</td>
<td>
<input type="submit" class="btn btn-info save ico" value="" name="edit" onClick="return confirm('Save For Sure!?')" />
<input type="hidden" name="_method" value="POST"/>
</td>
</tr>
</form>
<% } %>
javascript node.js express ejs multer
uploading 2 images with req.files[0].filename
, req.files[1].filename,
i can edit and update the first record only with req.files[0].filename
, req.files[1].filename,
NOTE i can upload and save images then read it from mongodb but when i want to update it, it's only update the first 2 images with this position
req.files[0].filename
, and req.files[1].filename
Here is my code:
1. uploading 2 images and everything goes alright
router.post('/upload', upload.any(), function(req, res) {
if (!req.body && !req.files) {
req.flash('error', 'error');
res.redirect('admin/home');
} else {
var c;
Photo.findOne({}, function(err, data) {
if (data) {
c = data.unique_id + 1;
} else {
c = 1;
}
var oto = new Photo({
unique_id: c,
smallcaption: req.body.caption,
smallpath: req.files[0].filename,
bigyear: req.body.year,
bigtitle: req.body.title,
bigcategory: req.body.category,
bigpath: req.files[1].filename,
});
oto.save((err, Person) => {
if (err) return err;
console.log(req.files);
req.flash('success_msg', 'Your Project Has Been Saved !!!');
res.redirect('admin/home');
});
})
.sort({ _id: -1 })
.limit(1);
}
});
****2. editing (Updating) my 2 images but only first record is updated****
router.post('/edit-project/(:id)', upload.any(), function(req, res, next) {
var otos = {
smallcaption: req.sanitize('smallcaption').trim(),
smallpath: req.files[0].filename,
bigyear: req.sanitize('bigyear').trim(),
bigtitle: req.sanitize('bigtitle').trim(),
bigcategory: req.sanitize('bigcategory').trim(),
bigpath: req.files[1].filename,
};
var o_id = new ObjectId(req.params.id);
db.collection('photos').update({ _id: o_id }, otos, function(err, result) {
if (err) {
req.flash('error_msg', 'Error Occurred When You Updating Your Data !!!');
res.render('admin/edit-project', {
smallcaption: req.body.smallcaption,
smallpath: req.files[0].filename,
bigyear: req.body.bigyear,
bigtitle: req.body.bigtitle,
bigcategory: req.body.bigcategory,
bigpath: req.files[1].filename,
});
} else {
console.log(req.files);
req.flash('success_msg', 'Data updated successfully !!!');
res.redirect('/admin/home');
}
});
});
3. uploading form
<div id="newproject" class="tabcontent">
<div class="col-lg-6 col-md-6 col-sm-10 col-xs-10">
<form id="proj-form" action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input id="s-caption" class="form-control" type="text" name="caption" placeholder="Small Photo Caption">
<input type="file" name="smallimg" id="smallimageUpload" class="hide"/>
<label for="smallimageUpload" class="btn btn-primary">Choose A Small Photo</label>
<img src="" id="smallimagePreview" alt="" class="imageview"/>
<input id="p-year" class="form-control" type="text" name="year" placeholder="Project Year">
<input id="p-title" class="form-control" type="text" name="title" placeholder="Project Title">
<input id="p-Cate" class="form-control" type="text" name="category" placeholder="Project Category">
<input type="file" name="bigimg" id="bigimageUpload" class="hide"/>
<label for="bigimageUpload" class="btn btn-primary">Choose A Big Photo</label>
<img src="" id="bigimagePreview" alt="" class="imageview"/>
<button type="submit" class="btn btn-info">Add Project</button>
<input type="button" class="btn btn-basic" onclick="resetProject()" value="Reset">
</div>
</form>
</div>
<div class="col-lg-3 col-md-3 col-sm-1 col-xs-1"></div>
</div>
4. editing Form
<% for(var i=0; i<fotos.length; i++) {%>
<form method="POST" action="/edit-project/<%= fotos[i]._id %>" enctype="multipart/form-data">
<tr>
<td><%= i+1 %></td>
<td><input type="text" class="caption-wi" name="smallcaption" value="<%= fotos[i].smallcaption %>"></td>
<td><div class="thumbnail">
<input type="file" name="smallimg" id="simgUpload" class="hide"/>
<label for="simgUpload" class="btn btn-primary">Small Photo</label>
</div></td>
<td><input type="text" class="year-wi" name="bigyear" value="<%= fotos[i].bigyear %>"></td>
<td><input type="text" class="update-wi" name="bigtitle" value="<%= fotos[i].bigtitle %>"></td>
<td><input type="text" class="update-wi" name="bigcategory" value="<%= fotos[i].bigcategory %>"></td>
<td><div class="thumbnail">
<input type="file" name="bigimg" id="bimgUpload" class="hide"/>
<label for="bimgUpload" class="btn btn-primary">Big Photo</label>
</div></td>
<td class="order-value">
<input class="" type="text" value="<%= i+1 %>">
</td>
<td>
<input type="submit" class="btn btn-info save ico" value="" name="edit" onClick="return confirm('Save For Sure!?')" />
<input type="hidden" name="_method" value="POST"/>
</td>
</tr>
</form>
<% } %>
javascript node.js express ejs multer
javascript node.js express ejs multer
edited Jan 2 at 8:25
Ani lastikyan
asked Dec 29 '18 at 13:27


Ani lastikyanAni lastikyan
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969993%2fupload-with-multer-2-images-then-when-edit-images-only-update-the-first-record%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969993%2fupload-with-multer-2-images-then-when-edit-images-only-update-the-first-record%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown