如何解决typeerror:config.query不是一个函数

pvcm50d1  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(363)

我对node.js和mssql还很陌生。在索引文件中查找我在config.query中遇到的错误。我尝试用mysql代码编写类似的代码。登录页面工作正常,但是当我尝试注册时,我得到了错误config.query不是一个函数。对html页面使用pug。这是app.js文件

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var config = require('./database/config');
var session = require('express-session');

var app = express();

app.use(session({
  secret : 'ABCDefg',
  resave : false,
  saveUninitialized : true
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

//Create Server
app.listen(3000,()=> {
  console.log('Listening on port 3000...');
});
module.exports = app;

这是index.js文件

var express = require('express');
var router = express.Router();
var mssql = require('mssql');
var bcrypt = require('bcrypt');
var config = require('../database/config');

/* GET home page. */
router.get('/', function(req, res, next) {
  if(req.session.flag ==1 ){
    req.session.destroy();
    res.render('index', { title: 'CodeLanguage',message: 'Email Already Exist', flag : 1 });

  }else if (req.session.flag ==2 ){
    req.session.destroy();
    res.render('index', { title: 'CodeLanguage',message: 'Registration Done. Please Login', flag : 0 });
  }
  else if (req.session.flag ==3 ){
    req.session.destroy();
    res.render('index', { title: 'CodeLanguage',message: 'Confirm Password Does Not Match.', flag : 1 });
  }else if (req.session.flag ==4 ){
    req.session.destroy();
    res.render('index', { title: 'CodeLanguage',message: 'Incorrect Email or Password', flag : 1  });
  }else{
    res.render('index', { title: 'CodeLanguage' });
  }

    });
//handle POST request for User Regisitratşon
router.post('/auth_reg',function(req,res,next){
  var fullname = req.body.fullname;
  var email = req.body.email;
  var password = req.body.password;
  var cpassword = req.body.cpassword;

  if(cpassword == password){
    var sql = 'select*from user where email = ?;' ;

    config.query(sql,[email],function(err,result,fields){
      if(err) throw err;

      if(result.length > 0){
        req.session.flag= 1;
        res.redirect('/');
      }
      else {

        var hashpassword = bcrypt.hashSync(password,10);
        var sql = 'insert into user(fullname,email,password) values(?,?,?)';
        config.query(sql,[fullname,email, hashpassword], function(err,result,fields){
          if(err) throw err;
          req.session.flag = 2;
          res.redirect('/');
        });
      }
    });
  }else{
    req.session.flag =3 ;
    res.redirect('/');
  }
});

//Handle POST request for User Login
router.post('/auth_login', function(req,res,next){
  var email = req.body.email;
  var password = req.body.password;
  var sql = 'select*from user where email = ?;';

  config.query(sql,[email], function(err,result,fields){
    if (err) throw err;

    if(result.length && bcrypt.compareSync(password,result[0].password)){  
      req.session.email = email;
      res.redirect('/home');
    }else{
      req.session.flag=4;
      res.redirect('/');
    }
  });
});

//Route for home page
router.get('/home',function(req,res,next){
  res.render('/home', {message: 'Welcome, '+ req.session.email});
});

router.get('/logout', function(req,res,next){
  if(req.session.email){
    req.session.destroy();
  }  
  res.redirect('/');
});
module.exports = router;

这是config.js文件

var mssql = require ('mssql');
var express = require('express');
var Connection = require('tedious').Connection;  

var config = {
    user: 'sa',
    password: '1',
    server: 'LAPTOP-US1RSKFM\SQLEXPRESS',  
    database: 'login_sys'
};
var connection = new Connection(config);  
connection.on('connect', function(err) {  
      // If no error, then good to proceed.
    console.log("Connected");  
});

connection.connect();

module.exports = config;
soat7uwm

soat7uwm1#

config.query 不是一个函数,因为 config.query 没有定义。你没有包括一个 query 属性定义和导出时 config config.js中的对象

var config = {
    user: 'sa',
    password: '1',
    server: 'LAPTOP-US1RSKFM\SQLEXPRESS',  
    database: 'login_sys',
    // query: () => ...
};

相关问题