Questa classe viene utilizzata per estendere tutti i controller all'interno della mia applicazione expressjs:ES6 classe, la funzione passa come parametro di
import response from '../utils/responseParser.js';
const APISLUG = '/api/v1/';
export default class BaseController {
constructor(name, app, model){
this.model = model;
this.app = app;
this.name = name;
console.log(model);
this.register();
}
register() {
this.app.get(APISLUG + this.name, this.all);
}
/*
Retrive all records
*/
all(req, res, next) {
this.model.all(req.body, (err, data) => {
if(err) return res.json(response.replyError(data));
return res.json(response.reply(data));
});
}
}
Come potete vedere ho fatto un metodo di "registrare" per set-up automaticamente tutti i percorsi di base.
ottengo un errore di unable to read property " model " of undefined "
su questa linea:
this.app.get(APISLUG + this.name, this.all);
Credo che questo sia dovuto al fatto che la portata perdersi quando passo la funzione come parametro. Come posso risolvere questo?
Grazie! Funziona. –
Prego :) –
'.bind (this)' è magico, grazie! – Robula