在课堂上使用其他方法时遇到问题。 我正在使用快速框架,必须使用Ecma Script 6。 当我尝试在同一个类中使用方法时,我有一个错误:
无法读取属性' generatePassword'未定义的
TypeError:无法读取属性' generatePassword'未定义的 在createUser(E:\ OpenServer \ domains \ testNode \ app \ controllers \ users.js:5:13) 在Layer.handle [as handle_request](E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ route.js:131:13) 在Route.dispatch(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [as handle_request](E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ layer.js:95:5) 在E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ index.js:277:22 在Function.process_params(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ index.js:330:12) 在下一个(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ index.js:271:10) 在Function.handle(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ index.js:176:3) 在路由器(E:\ OpenServer \ domains \ testNode \ node_modules \ express \ lib \ router \ index.js:46:12)
我的代码:
路由:/routes/users.js
import shutil, os, datetime, time, sys
newFolderName = datetime.datetime.now()
newFolder = newFolderName.strftime('%m%d%Y')
src = input('type the src dir: ')
dst= input('type the dest dir: ' +
newFolderName.strftime('%d-%B-%y\\').upper())
shutil.copytree(src, dst)
控制器:/app/controllers/users.js
const express = require('express');
const router = express.Router();
const users = require('../app/controllers/users');
router.get('/', users.createUser);
module.exports = router;
主应用程序文件:/app.js
class Users
{
createUser(req, res)
{
this.generatePassword();
res.json({msg: "The method return user data" });
}
generatePassword()
{
return 'The method return new password';
}
}
module.exports = new Users();
的package.json
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const index = require('./routes/index');
const users = require('./routes/users');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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');
});
module.exports = app;
请帮帮我,谢谢。
答案 0 :(得分:2)
在导致错误的行中,对象this
为not what you think it is。要解决此问题,请替换以下内容:
router.get('/', users.createUser);
使用:
router.get('/', users.createUser.bind(users));
这将确保router.get
实现最终调用该函数时,将this
对象设置为users
来调用它。