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
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
add a comment |
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
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
How ismulter
configured?filename
is only available if you're usingDiskStorage
, 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
add a comment |
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
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
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
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
javascript mysql node.js
asked Nov 18 at 14:22
sangumee
425
425
How ismulter
configured?filename
is only available if you're usingDiskStorage
, 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
add a comment |
How ismulter
configured?filename
is only available if you're usingDiskStorage
, 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
add a comment |
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.
Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
– sangumee
Nov 19 at 1:06
add a comment |
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.
Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
– sangumee
Nov 19 at 1:06
add a comment |
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.
Thanks for the help! It works now.!! :) I forgot about the conditional ternary operator.
– sangumee
Nov 19 at 1:06
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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%2f53361904%2fignore-undefined-data-in-mysql-update-with-nodejs%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
How is
multer
configured?filename
is only available if you're usingDiskStorage
, 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