Skip to content

FIX - postman testing issue gives error if {"admin": false} is not included in json body #214

Open
TracyChacon wants to merge 1 commit intobradtraversy:masterfrom
TracyChacon:patch-1
Open

FIX - postman testing issue gives error if {"admin": false} is not included in json body #214
TracyChacon wants to merge 1 commit intobradtraversy:masterfrom
TracyChacon:patch-1

Conversation

@TracyChacon
Copy link

@TracyChacon TracyChacon commented Mar 30, 2023

DETAILS

  • Chapter: 11 Admin Screens -Part 1
  • Video: Getting a User by ID and Updating User Endpoints
  • TimeStamp: 07:25
  • Location : userController.js file, line 153, inside the ````updateUser``` function.

ISSUE

When testing the backend using postman with the codeuser.isAdmin = req.body.isAdmin in userConroller.js the below error error appears

  • Error: "message": "User validation failed: isAdmin: Path isAdmin is required.",
    "stack": "ValidationError: User validation failed: isAdmin: Path isAdmin is required.\n at model.Document.invalidate

USECASE

The following code in the body section of postman causes the the error

{ 
"name":  "jane dolittle"
}

TEMP FIX

An ad hoc fix is including the isAdmin value. The below code allows the tester to update user values.

{ 
"name":  "jane dolittle",
"isAdmin": false 
}

PROPOSED CHANGE

The proposed code change makes it so that you don't need to include the isAdmin value in the JSON to fix the issue.

Change, in updateUser() function, line 153 from :

user.isAdmin = req.body.isAdmin

To:

user.isAdmin = Object.is(req.body.isAdmin, undefined)
      ? user.isAdmin
      : req.body.isAdmin

This makes so that the ```isAdmin```  value can now be updated 
Change to  line 153 from :
user.isAdmin = req.body.isAdmin
To:   
user.isAdmin = Object.is(req.body.isAdmin, undefined)
      ? user.isAdmin
      : req.body.isAdmin
@TracyChacon TracyChacon changed the title userController.js line 153 fix truthy/falsy postman testing issue gives error if {"admin": false} is not included in json body Apr 1, 2023
@TracyChacon TracyChacon changed the title postman testing issue gives error if {"admin": false} is not included in json body FIX - postman testing issue gives error if {"admin": false} is not included in json body Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant