gleber1
@gleber1

Где кроется ошибка в попытке наследования?

Это родительский класс
function MainController (){
    this.model = 'Main';
}
MainController.prototype = function() {
    return{
        findAll: function(req, res) {
            model.find({}, function (err, res) {
                if (err)  res.json({"findAll":err});
                res.json(res);
            });
        },
        findById : function(req,res) {
            var id = new ObjectID(req.params.id)
            model.findById(id, function (err, res) {
                if (err)  res.json({"findById":err});
                res.json(res);
            })
        },
        create: function(req, res){
            var saver = new model(req.body);
            saver.save(function(err,res){
                if (err) throw  err;
                res.json({'create':'yes'});
            });
        },
        update: function(req, res){

        }
    }

};
module.exports = MainController;

Тут у нас дочерний
var util = require('util');
var User = require('../models/user').User;
ObjectID = require('mongodb').ObjectID;
var mainController = new require('./parent/index')();
var UserController = function() {
    var model = 'User';
    return{
        checkAuth : function(req, res, next){
            if (req.session.user) {
                res.json({"login": req.session.user});
            }
            else{
                next();
            }
        },
        login: function(req, res, next){
            var mail = req.body.mail;
            var password  = req.body.password;
            User.autorize(mail,password,function(err, result, user){
                    if (result.login == 'yes'){
                        req.session.user = user._id;
                        res.json(result);
                    }
                    else{
                        res.json({"login":"no"});
                    }
                }
            )
        }
    }

};
util.inherits(UserController, mainController);
module.exports = UserController;

ну вы поняли.. Предполагается получить все родительские методы
но что то не задалось
util.js:556
ctor.prototype = Object.create(superCtor.prototype, {
^
TypeError: Cannot read property 'prototype' of undefined
at Object.exports.inherits (util.js:556:43)
at Object. (/home/azureuser/testproject/controllers/user.js:34:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at new require (module.js:380:17)
at Object. (/home/azureuser/testproject/routes/index.js:1:84)
at Module._compile (module.js:456:26)
azureuser@testonlyy:~/testproject$
  • Вопрос задан
  • 427 просмотров
Решения вопроса 1
k12th
@k12th
console.log(`You're pulling my leg, right?`);
Количество ошибок просто зашкаливает. Из какого-то кривого источника копипастили, да?(
Попробуйте вот так (не тестил):

// Родительский

function MainController () {
    this.model = 'Main';
}
MainController.prototype = {
    constructor: MainController,
    findAll: function (req, res) {
        this.model.find({}, function (err, res) {
            if (err)  res.json({"findAll": err});
            res.json(res);
        });
    },
    findById: function (req, res) {
        var id = new ObjectID(req.params.id);
        this.model.findById(id, function (err, res) {
            if (err)  res.json({"findById": err});
            res.json(res);
        })
    },
    create: function (req, res) {
        var saver = new this.model(req.body);
        saver.save(function (err, res) {
            if (err) throw  err;
            res.json({create: 'yes'});
        });
    },
    update: function (req, res) {

    }
};

module.exports = MainController;

// дочерний
var util = require('util');
var User = require('../models/user').User;
ObjectID = require('mongodb').ObjectID;
var MainController = require('./parent/index')();

var UserController = function () {
    this.model = User;
};

UserController.prototype = {
    prototype: UserController,
    checkAuth: function (req, res, next) {
        if (req.session.user) {
            res.json({login: req.session.user});
        } else {
            next();
        }
    },
    login: function (req, res, next) {
        var mail = req.body.mail;
        var password = req.body.password;
        User.autorize(mail, password, function (err, result, user) {
            if (result.login == 'yes') {
                req.session.user = user._id;
                res.json(result);
            }
            else {
                res.json({login: "no"});
            }
        }
        )
    }
};

util.inherits(UserController, MainController);
module.exports = UserController;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы