Why I am getting this error val.toString is not a function?

Viewed 715

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)
   
2 Answers

Probably you meant to pass "brand" or "data" to db.query() instead of res. So try "[brand, id]" or "[data, id]".

I think the parameters you pass in the second array should have a toString() method. This is because they are needed to be embedded into sql string. I came up with this idea using the stack trace of the error. The sqlstring library is trying to escape the parameters(for things like preventing sql injection), and there, it must be calling toString() on a parameter if it's not a string.

You are awaiting for a non async method since you use a callback instead. Just remove the await keyword from your code :

let result = db.query("Update Brands set ? where BrandId=?",[data, id], function(err,rows){...});

If you want to use the await keyword, update your code as follow :

try {
    let result = await db.query("Update Brands set ? where BrandId=?",[data, id]);
} catch (exception) {
    console.log("An error happened!" + exception);
}

As @srknzl mention, make sure your extra parameters [data, id] can be converted to string.

Related