Firebase에 연결하는 NodeJS 앱을 개발하려고합니다. 성공적으로 연결할 수 있지만 then
호출 범위를 관리하는 방법을 파악할 수 없습니다.NodeJS/Firebase 약속의 범위
내가 사용하고 NodeJS 6.9.2
내 테스트 구현은 다음과 같습니다
const EventEmitter = require('events');
const fb = require('firebase')
class FireGateway extends EventEmitter {
constructor() {
super();
if (this.instance) {
return this.instance;
}
// INIT
var fbConfig = {
apiKey: "xxxxx",
authDomain: "xxxxx.firebaseapp.com",
databaseURL: "https://xxxxx.firebaseio.com/"
};
fb.initializeApp(fbConfig)
this.instance = this;
this.testvar = "aaa";
}
login() {
fb.auth().signInWithEmailAndPassword ("email", "pwd")
.catch(function(error) {
// Handle Errors here.
}).then(function(onresolve, onreject) {
if (onresolve) {
console.log(this.testvar);
// "Cannot read property 'testvar' of undefined"
this.emit('loggedin');
// error as well
}
})
}
}
module.exports = FireGateway;
------
...
var FireGateway = require('./app/fireGateway');
this.fireGW = new FireGateway();
this.fireGW.login();
....
어떤 생각 나는 그것을 어떻게 관리 할 수 있습니까?