The below is my code for updating fields of db. I want to update the fields even when all the fields are not provided.It also has one image.I want to get it updated even when image is not given by the user.Please help me solve this error.
const updateBrand = async (req,res,next) => {
var id = req.params.id;
console.log(id)
var db = req.con;
const brand = {
"brandName" : req.body.brandName,
"sortOrder" : req.body.sortOrder,
"status" : req.body.status
}
// const updatedbrand = req.body;
if(req.files !== undefined){
const data = {
"brandName": req.body.brandName,
"sortOrder": req.body.sortOrder,
"status": req.body.status,
"brandImage": req.file.filename,
}
let result = await db.query("Update Brands set ? where BrandId=?",[data, id], function(err,rows){
if(err){
res.json({
success:0,
message:"An error occurred"
})
}
else{
res.json({
success:1,
message:"Updated"
})
}
})
}
if(req.files == undefined){
console.log("inside res")
console.log(brand)
let result = db.query("Update brands set ? where brandId=?",[res,id],function(err,rows){
if(err){
res.json({
success:0,
message:"An error occurred"
})
}
else{
res.json({
success:1,
message:"Updated"
})
}
})
}
}
And I am getting this response -
Server start on port : 8080
Connection established
15741804
inside res
{ brandName: 'sketcher', sortOrder: '5', status: 'Active' }
(node:10952) UnhandledPromiseRejectionWarning: TypeError: val.toString is not a function
at Object.escape (E:\coding backup\Brands\node_modules\sqlstring\lib\SqlString.js:52:33)
at Object.objectToValues (E:\coding backup\Brands\node_modules\sqlstring\lib\SqlString.js:180:89)
at Object.escape (E:\coding backup\Brands\node_modules\sqlstring\lib\SqlString.js:54:26)
at Object.format (E:\coding backup\Brands\node_modules\sqlstring\lib\SqlString.js:100:19)
at Connection.format (E:\coding backup\Brands\node_modules\mysql\lib\Connection.js:271:20)
at Connection.query (E:\coding backup\Brands\node_modules\mysql\lib\Connection.js:189:22)
at updateBrand (E:\coding backup\Brands\src\Controller\controller.js:204:25)
at Layer.handle [as handle_request] (E:\coding backup\Brands\node_modules\express\lib\router\layer.js:95:5)