Answer the question
In order to leave comments, you need to log in
How to pass a value to parent in the constructor?
The parent class that cannot accept model in any way, or rather, the console shows that this.model exists for itself and has all the functions with mongo
function MainController(model) {
this.model = model;
console.log(this.model);
}
MainController.prototype.findAll = function(req, res) {
this.model.find({}, function (err, results) {
if (err) res.json({"findAll": err});
res.json(results);
});
};
MainController.prototype.findById = function(req,res) {
var id = new ObjectID(req.params.id)
this.model.findById(id, function (err, results) {
if (err) res.json({"findById":err});
res.json(results);
})
};
MainController.prototype.create = function(req, res){
var saver = new this.model(req.body);
saver.save(function(err,results, affected){
if (err) throw err;
res.json({'create':'yes'});
});
};
module.exports = MainController;
var util = require('util');
var User = require('../models/user').User;
ObjectID = require('mongodb').ObjectID;
var MainController = require('./index');
function UserController () {
MainController.call(this,User);
this.model = User;
};
util.inherits(UserController, MainController);
UserController.prototype.checkAuth = function(req, res, next){
if (req.session.user) {
res.json({"login": req.session.user});
}
else{
next();
}
};
UserController.prototype.login = function(req, res, next){
var mail = req.body.mail;
var password = req.body.password;
model.autorize(mail,password,function(err, result, user){
if (result.login == 'yes'){
req.session.user = user._id;
res.json(result);
}
else{
res.json({"login":"no"});
}
}
)
};
module.exports = UserController;
var UserController = require('../controllers/user');
var userController = new UserController();
var uploader = require('../single/fileTransfer') ;
module.exports = function(app) {
app.get('/users',userController.findAll);
TypeError: Cannot call method 'find' of undefined
at MainController.findAll (/home/azureuser/test/controllers/index.js:7:24)
at Layer.handle [as handle_request] (/home/azureuser/test/node_modules/ express/lib/router/layer.js:82:5)
at next (/home/azureuser/test/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/home/azureuser/ test/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/home/azureuser/test/node_modules/express/lib/router/layer.js:82:5)
at /home/azureuser/test/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/home/azureuser/test/node_modules/express/lib/router/index.js:321 :12)
at next (/home/azureuser/test/node_modules/express/lib/router/index.js:261:10)
at /home/azureuser/test/node_modules/express-session/index.js:421:7
at /home /azureuser/test/node_modules/connect-mongo/lib/connect-mongo.js:295:11
Answer the question
In order to leave comments, you need to log in
I wouldn't be surprised if this replaces express. In general, the express documentation recommends closing midleware
MainController.prototype.findAll = function() {
var self = this;
return function (req, res) {
self.model.find({}, function (err, results) {
if (err) res.json({"findAll": err});
res.json(results); // вы забыли else, здесь будет ошибка повторной отправки данных!
});
}
};
/* -------------- */
app.get('/users', userController.findAll());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question