echology
echology

Reputation: 397

render another view after rendering a previous view in node.js with express

I am working on a node.js and express.js app.

I have router configured as:

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {

   // on load

   res.render('mail', { title: "this is the title." });

   // after some more funcitonality redirect/render a new view

 If(condition == 'success'){

   res.render('sent', { title: "this is the   title." });

   // or

   res.redirect('/sent');

  }


});

Now the problem is when I render a new view based on the condition after rendering the first view or if I redirect to a new route after rendering the first view, I get

Headers cannot be set after they have been sent error

The execution though continues but the view is not loaded and the page is not redirected.

Any help to overcome this issue will be much appreciated.

Upvotes: 0

Views: 860

Answers (1)

Carlos Fuentes
Carlos Fuentes

Reputation: 44

You can't send a second response.

Or you render a view or redirect to another route, not both.

Look in express for the documentation.

Upvotes: 1

Related Questions