Why Do I Get Entire Database When Only Requesting One Set?

When I make a POST request, I found out that it will give me the entire data base even though I just want one entry. Here is the code

app.get("/api/users", (req, res) => {
  User.find({}, (e, dt) => {
      //Easier to debug
      res.send('There was an error related to user search')
    } else if (!dt) {
      res.send('No such user exists')
      console.log(`This will log out even though I am doing a POST request and not a GET one`)

app.post("/api/users", (req, res) => {
  const newUser = new User({
    username: req.body.username

  newUser.save((e, dt) => {
    if (e) console.log(e)
    else res.json(dt)

This is your GET request, and it returns the collection’s documents - all of them as an array in the response. That is because, the User.find({}, method has a filter of {}.

This is your POST request. And, this saves the a user’s data and returns the newly saved user in the response.

I am aware of that, but why when I make a POST request, code in the GET request executes? When I run the code, “This will log out even though I am doing a POST request and not a GET one” gets displayed even though I didn’t make a GET request.

I am currently trying snippets out, here is the link

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.