Coder Social home page Coder Social logo

mongo_express's People

Contributors

israel025 avatar

Watchers

 avatar

mongo_express's Issues

Possibly unsetting already set fields to undefined

const updatedStudent = await StudentModel.findOneAndUpdate(
{ student_id: req.params.student_id },
{
first_name: req.body.first_name,
last_name: req.body.last_name,
gender: req.body.gender,
email: req.body.email,
class: req.body.class,
age: req.body.age,
},

In the following code for updating a student, the update argument which is an object could possibly be unsetting an already set field to undefined. Consider an instance where req.body.gender does not exist in the request body but the student already has a gender set, going by your code the student's gender field will be set to undefined because it does not exist in the request body. A better way to write the update argument is by simply sending the request body as the update argument. E.g

const updatedStudent = await StudentModel.findOneAndUpdate(
      { student_id: req.params.student_id },
      req.body,
      { new: true }
);

By doing that you're only sending the fields that are present in the request body as updates without tampering with fields that have already been set but may not exist in the request body. So in the instance where req.body is {"first_name": "Frank", "last_name": "Sinatra"} only the first_name and last_name fields will be sent as update arguments.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.