Я определил переменную класса в конструкторе, который содержит ссылку на импортированный экземпляр класса.
Однако, когда я пытаюсь получить доступ к переменной в моих методах класса, я продолжаю получать undefined
для этой переменной.
Экспортируемый класс
import models from '../../database/models'
const {User} = models;
class UserRepository {
constructor(){
this.model = User;
}
async create({firstname, lastname, email, isadmin, password}){
return await this.model.create({firstname, lastname, email, isadmin, password})
}
}
export default new UserRepository();
Класс, использующий экспортируемый класс
import repository from './repository';
import { sendSuccessMessage } from '../../utils'
class UserController {
constructor() {
this.model = repository;
}
async createUser({ body }, res, next) {
try {
const result = await this.model.create(body);
sendSuccessMessage(res, 200, result);
} catch( e ){
next(e)
}
}
}
export default new UserController();
Однако, используя переменную repository
каталог в createUser
как указано ниже:
const result = await repository.create(body);
работает как положено.
Текущая цепочка вызовов
// routes.js file
import controller from './controller'
export default [
{
path: '/signup',
method: 'post',
handlers: [controller.createUser] // createUser from class UserController
}
]
// utitlity.js file
export const routesLoader = (routes, router) => {
for (const route of routes) {
const { path, handlers, method } = route;
(router)[method](path, handlers);
}
};
// app.js file
import express from 'express';
import routes from './routes.js';
import { routesLoader } from './utils';
const router = express.Router();
routesLoader(routes, router);
// server.js file
import http from 'http';
import app from './app';
const { PORT = 3000 } = process.env;
const server = http.createServer(app);
server.listen(PORT, () => {
console.log(`server listening on localhost: ${PORT}`);
});
Ошибка
TypeError: Cannot read property 'create' of undefined
at _callee$ (C:\Users\MYPC\Downloads\moch-shop-api-master\components\user/controller.js:16:39)
at tryCatch (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:45:40)
at Generator.invoke [as _invoke] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:271:22)
at Generator.prototype.<computed> [as next] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\regenerator-runtime\runtime.js:97:21)
at asyncGeneratorStep (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)
at _next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:32:7
at new Promise (<anonymous>)
at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\@babel\runtime\helpers\asyncToGenerator.js:21:12
at createUser (C:\Users\MYPC\Downloads\moch-shop-api-master\components\user\controller.js:70:28)
at Layer.handle [as handle_request] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\express\lib\router\index.js:275:10)
at cors (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:188:7)
at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:224:17
at originCallback (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:214:15)
at C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:219:13
at optionsCallback (C:\Users\MYPC\Downloads\moch-shop-api-master\node_modules\cors\lib\index.js:199:9)
Используемые технологии
@babel/node: "^7.8.4"
terminal: git bash
Os: windows