一旦我将我的应用程序构建到Firebase托管中,我就会收到一些用户的错误:
@ firebase / database:FIREBASE警告:用户抛出了异常 打回来。 TypeError:无法读取属性' myID'未定义的
截图
我的代码中没有变量" myID",在任何地方。一切都可以很好地用于开发,但是一旦它被构建并在生产中,错误就会出现在某些用户身上。
这是我的packages.json版本:
"dependencies": {
"@angular-devkit/core": "0.0.29",
"@angular-devkit/schematics": "^0.6.0",
"@angular/animations": "^5.2.10",
"@angular/cli": "^1.7.4",
"@angular/common": "^5.2.10",
"@angular/compiler": "^5.2.10",
"@angular/core": "^5.2.10",
"@angular/forms": "^5.2.10",
"@angular/http": "^5.2.10",
"@angular/platform-browser": "^5.2.10",
"@angular/platform-browser-dynamic": "^5.2.10",
"@angular/router": "^5.2.10",
"@ng-bootstrap/ng-bootstrap": "^1.1.2",
"@schematics/package-update": "^0.6.0",
"ajv": "^6.0.0",
"angular2-recaptcha": "^0.6.0",
"angularfire2": "^5.0.0-rc.7",
"bootstrap": "^4.1.0",
"core-js": "^2.5.5",
"firebase": "^4.13.1",
"firebase-admin": "^5.10.0",
"firebase-functions": "^0.8.2",
"ng2-validation": "^4.2.0",
"rxjs": "^5.5.10",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^5.2.10",
"@angular/language-service": "^5.2.10",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^6.0.106",
"codelyzer": "^4.3.0",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.4.2",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~2.4.2"
}
修改 只有在我使用以下命令将其部署到firebase后运行我的应用程序时才会出现此问题:
ng build --prod
firebase deploy
我可以通过Chrome浏览器在源代码控制台中看到此代码:
(function(){var t={};return
t.id=this.myID,t.pw=this.myPW,t.ser=this.currentSerial,t}())
这是我的auth.service.ts。 仅在生产中调用函数Login()时抛出异常。
import { UserService } from './user.service';
import { AppUser } from '../models/app-user';
import { AngularFireAuth } from 'angularfire2/auth';
import { Injectable } from '@angular/core';
import * as firebase from 'firebase';
import { Observable } from 'rxjs/Observable';
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/of';
@Injectable()
export class AuthService {
user$: Observable<firebase.User>;
constructor(
private afAuth: AngularFireAuth,
private route: ActivatedRoute,
private userService: UserService) {
this.user$ = afAuth.authState;
}
login(){
let returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/';
localStorage.setItem('returnUrl', returnUrl);
this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
}
logout(){
this.afAuth.auth.signOut();
}
get appUser$(): Observable<AppUser>{
return this.user$
.switchMap(user => {
if (user) return this.userService.get(user.uid).valueChanges();
return Observable.of(null);
});
}
}
答案 0 :(得分:4)
以下是解决方法:
如果我使用:
修改我的login()方法 this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
而不是:
this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
我现在能够连接到生产中。这个问题的根源似乎是重定向功能。
答案 1 :(得分:3)
只需在app.component.ts中执行此操作
constructor() {
localStorage.removeItem('firebase:previous_websocket_failure');
}