Saturday 8 April 2017

express redirect error Can't set headers after they are sent

app.get('/', function (req, res) {
  mongoose.connect('mongodb://localhost/todo-list')
  Todo.find({completed: false}, function (err, todos) {
    if (err) throw err
    res.render('homepage', {
      allTodos: todos
    })
    mongoose.disconnect()
  })
})
//
app.post('/create', function (req, res) {
  // console.log(req.body)
  // res.send(req.body)
  todosController.create(req, res)
})

app.get('/listall', function (req, res) {
  res.redirect('/listall')
  todosController.list(req, res)
})
// and below is my controller

function list (req, res) {
  if (!mongoose.connection.db) mongoose.connect('mongodb://localhost/todo-list')
  Todo.find({}, function (err, todos) {
    if (err) throw err
    res.render('listall', {
      allTodos: todos
    })
    mongoose.disconnect()
  })
}

can someone tell me how to fix this >.< express shows error "Can't set headers after they are sent." at ServerResponse.setHeader (_http_outgoing.js:371:11)



via DarkArtistry

No comments:

Post a Comment