としてパス機能は、このクラスは、私のexpressjsアプリケーション内のすべてのコントローラを拡張するために使用されています。あなたは私が作った、自動的にセットアップするには、「登録」の方法を見ることができるようにES6クラス、パラメータ
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));
});
}
}
すべて基本ルート。
私は、この行のエラーunable to read property " model " of undefined "
を得る:
this.app.get(APISLUG + this.name, this.all);
私はこれは私がパラメータとして関数を渡す際にスコープが失われるという事実によるものであると信じています。これをどうすれば解決できますか?
ありがとうございました!できます。 –
あなたは大歓迎です:) –
'.bind(this)'は魔法です、ありがとう! – Robula