NodeJs项目Express + MySQL 提示:Can't set headers after they are sent.

错误提示:Can't set headers after they are sent.

错误意思:响应头文件未设置,数据已经发送

错误代码:

router.get('/', function(req, res, next) {
	var movies;
	db.DBConnection.query(
	'select * from movie',
	  function(err, result) {
		console.log(result);
		movies = result;
	  }
	);
   res.render('index.jade',{
        title:'网站首页',
        movies: movies
    });
});

正确代码:

//访问网站跟目录:localhost:3000/
router.get('/', function(req, res, next) {
	var movies;
	db.DBConnection.query(
	'select * from movie',
	  function(err, result) {
		return res.render('index.jade',{
			title:'网站首页',
			movies: result
		});
	  }
	); 
});

总结:MySQL 数据查询,直接在回调函数中,数据相关数据和页面

你可能感兴趣的:(nodejs)