A
A
Abc Edc2015-04-20 08:54:18
Node.js
Abc Edc, 2015-04-20 08:54:18

Where is the error in trying to inherit?

This is the parent class

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;

Here we have a child
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;

well, you understand .. It is supposed to get all parent methods,
but something didn’t work
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)
[email protected]:~/testproject$

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-04-20
@gleber1

The number of errors is simply overwhelming. From some crooked copy-paste source, right? (
Try like this (not tested):

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

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;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question