node.js - Error: Can't set headers after they are sent in node js MVC approach -
i getting following error ...
events.js:160 throw er; // unhandled 'error' event ^ error: can't set headers after sent. @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:357:11) @ serverresponse.header (c:\level3proj\node_modules\express\lib\response.js:730:10)
.. when execute following code
var mongoose = require('mongoose'); var express = require('express'); // express router used define routes var userrouter= express.router(); var usermodel = mongoose.model('user'); var allusers = []; var fs = require('fs'); module.exports.controller = function(app) { userrouter.get('/all',function(req,res) { res.send("this route information on particular "); usermodel.find({},function(err,allusers) { if(err) { res.send(err); } else { res.send(allusers); } }); // end user model }); // end users app.use('/users', userrouter); } // end controller code
when load
http://localhost:3000/users/all
the result presented , @ same throwing error in console.
why getting error on console?
you send responce 2 times. first 1 here
res.send("this route information on particular ");
and after here
if(err) { res.send(err); } else { res.send(allusers); }
remove first sending , responce work
Comments
Post a Comment