Ignore undefined data in MySQL UPDATE with NodeJS











up vote
1
down vote

favorite












I developed CRUD (Create, Read, Update, Delete) App with nodejs. But I have some problem in Update Function.



/* POST Update Page */
router.post(
"/:userId/:pageId/update_process",
upload.single("projectImg"),
function(req, res) {
let userId = req.params.userId;
let pageId = req.params.pageId;
db.query(`SELECT imgurl FROM Personal_Data WHERE id=?`, [pageId], function(
error,
data
) {
if (error) {
throw error;
}
console.log(data[0]);
let checkImage = data[0];

let name = req.body.projectName;
let type = req.body.portType;
let url = req.body.projectUrl;
let explanation = req.body.projectExplanation;

let imgurl = req.file.filename;
let sumlang = req.body.sumLang;
let pjdate1 = req.body.pjdate1;
let pjdate2 = req.body.pjdate2;
let githuburl = req.body.githuburl;

// If Imgurl is undefined
if (imgurl === undefined) {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
// If Imgurl is exist
} else {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, imgurl=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
imgurl,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
}
});

res.redirect("/" + userId);
}
);


All functions works fine but in imgurl part is not working properly. It's a update page so I get all the previous data from DB.So Update page looks like this



enter image description here



In file upload button the default data is null and if I update the with different image, it works fine. But I want to make it If I do not specify a new image file, I just want to use the old data as it is.



But If the file exist it works but if it's not exist it gives me an error.



TypeError: Cannot read property 'filename' of undefined









share|improve this question






















  • How is multer configured? filename is only available if you're using DiskStorage, for instance.
    – robertklep
    Nov 18 at 14:36












  • @robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
    – sangumee
    Nov 18 at 14:53















up vote
1
down vote

favorite












I developed CRUD (Create, Read, Update, Delete) App with nodejs. But I have some problem in Update Function.



/* POST Update Page */
router.post(
"/:userId/:pageId/update_process",
upload.single("projectImg"),
function(req, res) {
let userId = req.params.userId;
let pageId = req.params.pageId;
db.query(`SELECT imgurl FROM Personal_Data WHERE id=?`, [pageId], function(
error,
data
) {
if (error) {
throw error;
}
console.log(data[0]);
let checkImage = data[0];

let name = req.body.projectName;
let type = req.body.portType;
let url = req.body.projectUrl;
let explanation = req.body.projectExplanation;

let imgurl = req.file.filename;
let sumlang = req.body.sumLang;
let pjdate1 = req.body.pjdate1;
let pjdate2 = req.body.pjdate2;
let githuburl = req.body.githuburl;

// If Imgurl is undefined
if (imgurl === undefined) {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
// If Imgurl is exist
} else {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, imgurl=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
imgurl,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
}
});

res.redirect("/" + userId);
}
);


All functions works fine but in imgurl part is not working properly. It's a update page so I get all the previous data from DB.So Update page looks like this



enter image description here



In file upload button the default data is null and if I update the with different image, it works fine. But I want to make it If I do not specify a new image file, I just want to use the old data as it is.



But If the file exist it works but if it's not exist it gives me an error.



TypeError: Cannot read property 'filename' of undefined









share|improve this question






















  • How is multer configured? filename is only available if you're using DiskStorage, for instance.
    – robertklep
    Nov 18 at 14:36












  • @robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
    – sangumee
    Nov 18 at 14:53













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I developed CRUD (Create, Read, Update, Delete) App with nodejs. But I have some problem in Update Function.



/* POST Update Page */
router.post(
"/:userId/:pageId/update_process",
upload.single("projectImg"),
function(req, res) {
let userId = req.params.userId;
let pageId = req.params.pageId;
db.query(`SELECT imgurl FROM Personal_Data WHERE id=?`, [pageId], function(
error,
data
) {
if (error) {
throw error;
}
console.log(data[0]);
let checkImage = data[0];

let name = req.body.projectName;
let type = req.body.portType;
let url = req.body.projectUrl;
let explanation = req.body.projectExplanation;

let imgurl = req.file.filename;
let sumlang = req.body.sumLang;
let pjdate1 = req.body.pjdate1;
let pjdate2 = req.body.pjdate2;
let githuburl = req.body.githuburl;

// If Imgurl is undefined
if (imgurl === undefined) {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
// If Imgurl is exist
} else {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, imgurl=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
imgurl,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
}
});

res.redirect("/" + userId);
}
);


All functions works fine but in imgurl part is not working properly. It's a update page so I get all the previous data from DB.So Update page looks like this



enter image description here



In file upload button the default data is null and if I update the with different image, it works fine. But I want to make it If I do not specify a new image file, I just want to use the old data as it is.



But If the file exist it works but if it's not exist it gives me an error.



TypeError: Cannot read property 'filename' of undefined









share|improve this question













I developed CRUD (Create, Read, Update, Delete) App with nodejs. But I have some problem in Update Function.



/* POST Update Page */
router.post(
"/:userId/:pageId/update_process",
upload.single("projectImg"),
function(req, res) {
let userId = req.params.userId;
let pageId = req.params.pageId;
db.query(`SELECT imgurl FROM Personal_Data WHERE id=?`, [pageId], function(
error,
data
) {
if (error) {
throw error;
}
console.log(data[0]);
let checkImage = data[0];

let name = req.body.projectName;
let type = req.body.portType;
let url = req.body.projectUrl;
let explanation = req.body.projectExplanation;

let imgurl = req.file.filename;
let sumlang = req.body.sumLang;
let pjdate1 = req.body.pjdate1;
let pjdate2 = req.body.pjdate2;
let githuburl = req.body.githuburl;

// If Imgurl is undefined
if (imgurl === undefined) {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
// If Imgurl is exist
} else {
db.query(
`UPDATE Personal_Data SET name=?, type=?, url=?, explanation=?, imgurl=?, sumlang=?, pjdate1=?, pjdate2=?, githuburl=? WHERE id=?`,
[
name,
type,
url,
explanation,
imgurl,
sumlang,
pjdate1,
pjdate2,
githuburl,
pageId
]
);
}
});

res.redirect("/" + userId);
}
);


All functions works fine but in imgurl part is not working properly. It's a update page so I get all the previous data from DB.So Update page looks like this



enter image description here



In file upload button the default data is null and if I update the with different image, it works fine. But I want to make it If I do not specify a new image file, I just want to use the old data as it is.



But If the file exist it works but if it's not exist it gives me an error.



TypeError: Cannot read property 'filename' of undefined






javascript mysql node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 14:22









sangumee

425




425












  • How is multer configured? filename is only available if you're using DiskStorage, for instance.
    – robertklep
    Nov 18 at 14:36












  • @robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
    – sangumee
    Nov 18 at 14:53


















  • How is multer configured? filename is only available if you're using DiskStorage, for instance.
    – robertklep
    Nov 18 at 14:36












  • @robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
    – sangumee
    Nov 18 at 14:53
















How is multer configured? filename is only available if you're using DiskStorage, for instance.
– robertklep
Nov 18 at 14:36






How is multer configured? filename is only available if you're using DiskStorage, for instance.
– robertklep
Nov 18 at 14:36














@robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
– sangumee
Nov 18 at 14:53




@robertklep In multer it configured with DiskStorage so that I can use filename. But It only not works when the file is not exist
– sangumee
Nov 18 at 14:53












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I think this is what you want:



let imgurl = req.file ? req.file.filename : undefined;


In other words: if the user didn't upload a file, imgurl should be undefined, otherwise it should contain the name of the file.






share|improve this answer





















  • Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
    – sangumee
    Nov 19 at 1:06











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361904%2fignore-undefined-data-in-mysql-update-with-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I think this is what you want:



let imgurl = req.file ? req.file.filename : undefined;


In other words: if the user didn't upload a file, imgurl should be undefined, otherwise it should contain the name of the file.






share|improve this answer





















  • Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
    – sangumee
    Nov 19 at 1:06















up vote
1
down vote



accepted










I think this is what you want:



let imgurl = req.file ? req.file.filename : undefined;


In other words: if the user didn't upload a file, imgurl should be undefined, otherwise it should contain the name of the file.






share|improve this answer





















  • Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
    – sangumee
    Nov 19 at 1:06













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I think this is what you want:



let imgurl = req.file ? req.file.filename : undefined;


In other words: if the user didn't upload a file, imgurl should be undefined, otherwise it should contain the name of the file.






share|improve this answer












I think this is what you want:



let imgurl = req.file ? req.file.filename : undefined;


In other words: if the user didn't upload a file, imgurl should be undefined, otherwise it should contain the name of the file.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 15:04









robertklep

132k17227238




132k17227238












  • Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
    – sangumee
    Nov 19 at 1:06


















  • Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
    – sangumee
    Nov 19 at 1:06
















Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
– sangumee
Nov 19 at 1:06




Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
– sangumee
Nov 19 at 1:06


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361904%2fignore-undefined-data-in-mysql-update-with-nodejs%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin