Giter Site home page Giter Site logo

Comments (5)

FaisalAfroz avatar FaisalAfroz commented on July 20, 2024

5f6aab24c4bf8d504d6e826c

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on July 20, 2024

READ: User.findById

Great, your user has an id of 5f6aab24c4bf8d504d6e826c. This is what we will use in place of :id, as we make the other requests. find the "READ" route in our server file, and replace it with this code:

// READ
.get((req,res)=>{
  User.findById(req.params.id,(err,data)=>{
    if (err){
      res.json({
        success: false,
        message: err
      })
    } else if (!data){
      res.json({
        success: false,
        message: "Not Found"
      })
    } else {
      res.json({
        success: true,
        data: data
      })
    }
  })
})

Testing

Let's find the document for the user we just created. Make a "get" request in postman to this url: http://localhost:8000/users/5f6aab24c4bf8d504d6e826c. This should return the document with the user's data.

Once you have this working, push your code to GitHub to continue.

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on July 20, 2024

UPDATE: User.findByIdAndUpdate

If you want to update a document in mongoDB, you can do it with the User.findByIdAndUpdate method. This takes three arguments (id, newData, callback). The id is still coming from "req.params", but newData is an object sent through the "req.body". Also, by default the update method will return the unmodified document. We can add an "options" argument before the callback ({new:true}) to make it return the modified document.

Replace the "UPDATE" route with this code:

// UPDATE
.put((req,res)=>{
  User.findByIdAndUpdate(
    req.params.id,
    {
      name:req.body.newData.name,
      email:req.body.newData.email,
      password:req.body.newData.password
    },
    {
      new:true
    },
    (err,data)=>{
      if (err){
        res.json({
          success: false,
          message: err
        })
      } else if (!data){
        res.json({
          success: false,
          message: "Not Found"
        })
      } else {
        res.json({
          success: true,
          data: data
        })
      }
    }
  )
})

Test this out by making a "PUT" request in Postman at the endpoint for our user. Update the password field by putting this json data in the request body:

{
  "newData":{
    "name":"Jim",
    "email":"[email protected]",
    "password":"newPassword"
  }
}

If this worked, you should see the newly modified document as a response.

Push your code to GitHub for the next step.

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on July 20, 2024

Delete Data: findByIdAndDelete

To delete a document, you can use the User.findByIdAndDelete method, which takes an id and callback as arguments.

Replace the "delete" route with this:

// DELETE
.delete((req,res)=>{
  User.findByIdAndDelete(
    req.params.id,
    (err,data)=>{
      if (err){
        res.json({
          success: false,
          message: err
        })
      } else if (!data){
        res.json({
          success: false,
          message: "Not Found"
        })
      } else {
        res.json({
          success: true,
          data: data
        })
      }
    }
  )
})

To test this out, make a "delete" request in Postman to url we have been using.

You can test to make sure this worked by making a "get" request to the same url. It should say that the data could not be found.

Push your code to GitHub to complete this step.

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on July 20, 2024

Awesome! The last thing I want to go over is refactoring our routes to make them easier to maintain.

Click here to learn how.

from intermediate-node-course.

Related Issues (4)

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.