Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] - Getting Error while get a note #33

Open
pdd1996 opened this issue May 9, 2023 · 2 comments
Open

[Bug] - Getting Error while get a note #33

pdd1996 opened this issue May 9, 2023 · 2 comments

Comments

@pdd1996
Copy link

pdd1996 commented May 9, 2023

Description of the problem

  • Error - Terminal throw an error : “ Cannot set headers after they are sent to the client ” when i use GET method to get a note
  • Solution - delete response.json(note)
  • Screenshot of the error
  1. Postman
  2. Terminal
// Error Code
app.get('/api/notes/:id', (request, response) => {
  const id = Number(request.params.id)
  const note = notes.find(note => note.id === id)

  if (note) {
    response.json(note)
  } else {
    response.status(404).end()
  }
// delete this line
  response.json(note)
})

// Solution 
app.get('/api/notes/:id', (request, response) => {
  const id = Number(request.params.id)
  const note = notes.find(note => note.id === id)

  if (note) {
    response.json(note)
  } 

  if(!note){
    response.status(404).end()
  }
})

Reproduction Repo:

null

Node Version:

v16.19.1

Platform and Version:

win 11 22H2


Other

Good morning, teacher
I am a JavaScript learner from China,thanks for your open source projects and translate it in chinese,I gain a lot. that why I yearn for your university. however, I didn't study hard in college,my GPA is low now, so I can't apply to your university. this is my first official writing issue,if there are any problems, please point them out and provide information in the correct format, I will modify it next time. If you come to Hang Zhou(Zhe Jiang) to play,I will welcome you warmly.
Yours
China student
2023.5.10

@pdd1996
Copy link
Author

pdd1996 commented May 15, 2023

@mluukkai

@ayushchauhan101
Copy link

Nice work, try to use if else together instead of two separate if statements and check again if working correctly.
Create a push request later.

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

No branches or pull requests

2 participants