zoukankan      html  css  js  c++  java
  • TypeError: Router.use() requires a middleware function but got a Object

    1、错误描述

    C:UsersAdministrator.USER-0GUONPPBHK>cd G:HBuilderXWorkVueserver
    
    C:UsersAdministrator.USER-0GUONPPBHK>g:
    
    G:HBuilderXWorkVueserver>node index
    G:HBuilderXWorkVue
    ode_modulesexpresslib
    outerindex.js:458
          throw new TypeError('Router.use() requires a middleware function but got a
     ' + gettype(fn))
          ^
    
    TypeError: Router.use() requires a middleware function but got a Object
        at Function.use (G:HBuilderXWorkVue
    ode_modulesexpresslib
    outerindex.
    js:458:13)
        at Function.<anonymous> (G:HBuilderXWorkVue
    ode_modulesexpresslibappli
    cation.js:220:21)
        at Array.forEach (<anonymous>)
        at Function.use (G:HBuilderXWorkVue
    ode_modulesexpresslibapplication.j
    s:217:7)
        at Object.<anonymous> (G:HBuilderXWorkVueserverindex.js:11:5)
        at Module._compile (internal/modules/cjs/loader.js:721:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
        at Module.load (internal/modules/cjs/loader.js:620:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
        at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    
    G:HBuilderXWorkVueserver>

    2、错误原因

         在定义访问数据库,并进行数据操作,没有使用module.exports

    3、解决办法

    const express = require('express')
    const mysql = require('mysql')
    const dbConfig = require('./db')
    const sqlMap = require('./sqlMap')
    
    var router = express.Router();
    
    
    var conn = mysql.createConnection(dbConfig.mysql);
    conn.connect();
    
    var jsonWrite = function(res,req){
    	if(typeof req === 'undefined'){
    		res.json({
    			code: '1',
    			msg: '操作失败'
    		});
    	}else{
    		res.json(req);
    	}
    }
    
    router.post('/queryData',(res,req) => {
    	var sql = sqlMap.stu.select();
    	conn.query(sql, function(err,result){
    		if(err){
    			console.log(err);
    		}
    		if(result){
    			jsonWrite(res,result);
    		}
    	})
    });
    
    module.exports = router;
    
    
  • 相关阅读:
    打包的@font-face包
    带你零基础入门redis【二】
    带你零基础入门redis【一】
    CentOS查找文件命令
    输入密码时提示大写锁定已打开
    JSP界面设置提示浮动框
    极光推送
    MySQL获取汉字的首字母
    mySQL函数根据经纬度计算两点距离
    js判断输入时间是否大于系统时间
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313701.html
Copyright © 2011-2022 走看看