为什么我的代码中出现了与“不存在”相关的与methot相关的错误?

时间:2020-09-30 12:01:45

标签: javascript angular firebase rxjs

我不太喜欢Angular和RxJS,经过一些重构,我发现此服务类存在以下问题:

从'@ angular / core'导入{可注射};

import { Router } from '@angular/router';
import { BehaviorSubject, Observable } from 'rxjs';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { finalize, switchMap } from 'rxjs/operators';
import { AngularFireStorage } from '@angular/fire/storage';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private eventAuthError = new BehaviorSubject<string>("");
  eventAuthError$ = this.eventAuthError.asObservable();

  newUser: any;

  downloadURL: Observable<string>;
  fb;

  constructor(
    private afAuth: AngularFireAuth,
    private db: AngularFirestore,
    private router: Router,
    private storage: AngularFireStorage
    ){ }

    getUserState() {
      return this.afAuth.authState;
    }

    login( email: string, password: string) {
      this.afAuth.auth.signInWithEmailAndPassword(email, password)
    .catch(error => {
      this.eventAuthError.next(error);
    })
    .then(userCredential => {
      if(userCredential) {
        this.router.navigate(['/home']);
      }
    })
    }

    createUser(user, file) {
      console.log(user);

      if(user.password != user.password_confirmation) {
    let passwordError: any = new Object();
    passwordError["message"] = "Inserted password and inserted password confirmation are different";
    this.eventAuthError.next(passwordError);
      }
      else {
    this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
      .then( userCredential => {
        this.newUser = user;
        console.log(userCredential);
        userCredential.user.updateProfile( {
          displayName: user.firstName + ' ' + user.lastName
        });

        this.uploadFileIntoFirebaseStore(file).subscribe(downloadURL =>
          this.insertUserData(userCredential, downloadURL)
            .then(() => {
              this.router.navigate(['/home']);
            });
          );
      })
      .catch( error => {
        console.log(error);
        console.log(typeof error);
        this.eventAuthError.next(error);    // Emit the event passing the error object
      });
    }
    }

    uploadFileIntoFirebaseStore(fileToBeUploaded) {
      var n = Date.now();
      const filePath = `user_avatar/${n}`;
      const fileRef = this.storage.ref(filePath);

      return this.storage.upload(`user_avatar/${n}`, fileToBeUploaded)
        .snapshotChanges()
        .pipe(switchMap(() => fileRef.getDownloadURL()));

    }

    insertUserData(userCredential: firebase.auth.UserCredential, userAvatar) {

      let userObg = {
        name: this.newUser.firstName,
        surname: this.newUser.lastName,
        complete_name: this.newUser.firstName + " " + this.newUser.lastName,
        email: this.newUser.email,
        role: this.newUser.ruolo_utente,
        avatar: userAvatar
      };

      console.log("USER OBJECT: ", userObg);

      return this.db.doc(`Users/${userCredential.user.uid}`).set(userObg);
    }

    logout() {
      return this.afAuth.auth.signOut();
    }
}

我的问题与 createUser()方法有关。

IDE在我的代码中给了我3个错误(可能是因为我仍然与反应式编程风格有关……)

  1. this.uploadFileIntoFirebaseStore(file)上调用它会给我以下错误消息:

属性“ uploadFileIntoFirebaseStore”在类型上不存在 'AuthService'.ts(2339)

但是您可以看到,此方法已定义在我的 AuthService 服务类中。

  1. this.insertUserData(userCredential,downloadURL) cal的类似问题,给我带来类似的错误:

属性“ insertUserData”在类型上不存在 'AuthService'.ts(2339)

  1. 最后,我在此渔获时出错:

    .catch(错误=> { console.log(错误); console.log(typeof错误); this.eventAuthError.next(error); //发出传递错误对象的事件 });

IDE说:

希望“尝试”。ts(1005)

但这应该引起错误,发生在然后

我的代码有什么问题?我想念什么?我该如何解决?

1 个答案:

答案 0 :(得分:3)

一个;太多了:)

 this.uploadFileIntoFirebaseStore(file).subscribe(downloadURL =>
          this.insertUserData(userCredential, downloadURL)
            .then(() => {
              this.router.navigate(['/home']);
            });
          );

应该是

 this.uploadFileIntoFirebaseStore(file).subscribe(downloadURL =>
          this.insertUserData(userCredential, downloadURL)
            .then(() => {
              this.router.navigate(['/home']);
            })
          );