User entries not updating in database

Viewed 530

I am using postman to send a request and I see Success message but in the database, it's not updated at all.

PostMAN request PostMan request


database Snap shot enter image description here


update services object: from this file I have used a database query to insert data in the database and set callBack funtion
const pool = require('../../config/database')

module.exports = {
updateUser: (data, callBack) => {
    pool.query(
      `UPDATE users SET firstName=?,email=?,password=?,lastName=?,phoneNumber=?, sex=? WHERE id=?`, [
        data.firstName,
        data.email,
        data.password,
        data.lastName,
        data.phoneNumber,
        data.sex,
        data.id
      ], (error, results, fields) => {
        if (error) {
          return callBack(error)
        }
        return callBack(null, results)
      }
    )
  }
}

update user controller here I have added a controller to update the user details which receive the data from update user services.

const {
   create,
   getUserbyID,
   getUsers,
   updateUser,
   deleteUser,
   getUserByEmail
 } = require('./userService')
const {genSaltSync, hashSync, compareSync} = require('bcrypt')
const { sign } = require('jsonwebtoken')

module.exports ={
updateUser: (req, res) => {
    const body = req.body;
    const salt = genSaltSync(10);
    body.password = hashSync(body.password, salt);
    updateUser(body, (err, results) => {
      if (err) {
        console.log(err)
        return false;
      } // added
      console.log("this is the body: "+JSON.stringify(req.body))
      console.log("this is the results: "+ JSON.stringify(results))
      if (!results) {
        return res.json({
          success:0,
          message: "failed to update user"
        })
      }
      return res.json({
        success: 1,
        message: "Updated Sucessfully"
      })
    })
  },
}

router.js

router.patch('/update',checkToken, updateUser)

ADDED console.log

this is the body: {"Id":15,"firstName":"joey","email":"joey.chandler357@gmail.com","password":"$2b$10$ZBnRppSKAfQ1TrzGvs/wqOrVx/shb6ESJ7emXnC7IlWRN3VUGgfK2","lastName":"chandler","phoneNumber":"9860316634","sex":"Male"}
this is the results: {"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":2,"warningCount":0,"message":"","protocol41":true,"changedRows":0}
3 Answers

I can see your console.log message

this is the results: {"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":2,"warningCount":0,"message":"","protocol41":true,"changedRows":0}

Here you can notice affectedRows: 0 it means no row updated this happens when condition is not matched with any of the records. In postman you are passing "Id" I is in capital format but at the time of accessing this in service you are using "data.id" id is small latter so this is creating problem

we can handle this instead of if (!results) { return res.json({ success:0, message: "failed to update user" }) } use

if (!results.affectedRows) {
    return res.json({
      success:0,
      message: "failed to update user"
    })
  }

this will be much better then previous check

I think you need to use an "insert" to add the db record. It's using an update... so it's looking for a pre-existing record.

Try two things:

  1. wrap “users” in quotes on your update query. I’ve seen this w Postgres where some words are reserved in raw queries.
  2. Examine the database response from your update. See what is console logged.
Related