0
votes

As i am new to nodejs. I keep getting the following error code Error : Error: ER_BAD_FIELD_ERROR: Unknown column 'user' in 'where clause'

i am using nodejs with mysql and express. i want to edit the user details but im getting this above error.

    app.get('/edit', function(req, res, next){
    var model = new User(connection);
    var user={ id: req.params.id}

    model.edit({user}, function(err, rows, fields)
    {

        // if user not found
        if (rows.length <= 0) {
            console.log('error', 'User not found with id = ' + 
       req.params.id)
            res.redirect('home')
        }
        else { // if user found
            // render to views/user/edit.ejs template file
            res.render('user/edit', {
                title: 'Edit User',
                //data: rows[0],
                id: rows[0].id,
                name: rows[0].name,
                password: rows[0].password,
                email: rows[0].email
            })
        }

       })

    });
  -----------------------------
  Query:
    User.prototype.edit = function(id) 
 {
    connection= this.connection;
    connection.query('SELECT * FROM users WHERE id = ? ',[id],function(err, 
  result, fields) {
    //if(err) throw err
    if(err)
    {
        console.log("Error  : %s ",err );
        return false
    }
    console.log("Result", result);
    return result;
   })

}

thanks for help...

1

1 Answers

0
votes

Your edit function seems to expect just the id but your pass an object. Assuming req.params.id really contains the id, try one of the following possibilities:

  1. edit the router file:

    model.edit( req.params.id , function(err, rows, fields){ ... })
    
    // or 
    var user={ id: req.params.id}
    model.edit( user.id , function(err, rows, fields){ ... })
    

    OR

  2. edit both router and the model file:

    // router file
    var user={ id: req.params.id}
    
    model.edit(user, function(err, rows, fields){ ... })
    
    // model file
    User.prototype.edit = function(user_id_object){
        connection.query('SELECT * FROM users WHERE id = ? ',[user_id_object.id],function(err, result, fields) { .... })
     }