TypeError: res.render is not a function

node + mysql:

When writing the demo today, I found that the code reported an error typeerror: res.render Is not a function, but I tested myself when it is not suitable for MySQL to query data res.render The () method is available, but when MySQL data is not available res.render() will not go wrong. Direct code:

Problem code

var express = require('express');
var router = express.Router();
const mysqlCon = require('../modules/mysql');

router.get('/', function(req, res, next) {
  mysqlCon.query("SELECT empno,ename,job FROM emp WHERE empno > 1008",function(err,res){
    if(err){
      return res.redirect('/');
    }
    console.log('======================res==========================');
    var UserInfo = res;
    // UserInfo = JSON.parse(JSON.stringify(UserInfo))
    res.render('index', {
			title: 'Home Page',
			UserInfo
		})
  })
});

report errors

Solution:

var express = require('express');
var router = express.Router();
const mysqlCon = require('../modules/mysql');

router.get('/', function(req, res, next) {
  mysqlCon.query("SELECT empno,ename,job FROM emp WHERE empno > 1008",function(err,result){
    if(err){
      return res.redirect('/');
    }
    console.log('======================res==========================');
    var UserInfo = result;
    // UserInfo = JSON.parse(JSON.stringify(UserInfo))
    res.render('index', {
			title: 'Home Page',
			UserInfo
		})
  })
});

The return value of MySQL query, res, can be changed to other definitions. Because the previous code style habitually defines the return value as res, which conflicts with the corresponding res in the node project res.render is not a function。 It’s not a big pit, mainly because the code habit has not been transformed…

Read More: