当前位置: 首页 > 知识库问答 >
问题:

如何修复错误TS2339:类型上不存在属性“auth token”

孔飞舟
2023-03-14

我正在开发一个基于Firabase实时数据库的离子应用程序。当尝试启动应用程序时,im会收到此错误消息。

[ng]src/app/data-service.service.ts(14,36)中出现错误:错误TS2339:类型“{apikey:String;authDomain:String;databaseUrl:String;ProjectId:String;StorageBucket:String;MessagingSenderId:String;}”上不存在属性“auth token”。

看来Firebase验证有问题。但我不知道该怎么解决。

数据-service.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment  } from '../environments/environment';
import * as firebase from 'firebase/app';


@Injectable({
  providedIn: 'root'
})
export class DataServiceService {
  userId: any;
  userName: any;
  databaseUrl = environment.firebase.databaseURL;
  authToken = environment.firebase.authToken;

  constructor(private http: HttpClient) {
    console.log('Hello DataServiceService Provider');
    console.log(this.databaseUrl);
    console.log(this.authToken);
    this.userId = localStorage.getItem('userId');
    if (!localStorage.getItem('currentUserShift')) {
      localStorage.setItem('currentUserShift', JSON.stringify({ periods: {} }));
    }
    if (!localStorage.getItem('pendingShifts')) {
      localStorage.setItem('pendingShifts', JSON.stringify([]));
    }
  }

  setUserId(userId) {
    console.log('setUserId ');
    this.userId = userId;
    localStorage.setItem('userId', userId);
  }

  getUserId() {
    return this.userId;
  }

  getUserName() {
    return this.userName;
  }

  setUserName(userName) {
    this.userName = userName;
  }

  updateLocalCurrentUserShift(currentUserShift) {
    localStorage.setItem('currentUserShift', JSON.stringify(currentUserShift));
  }

  getLocalCurrentUserShift() {
    return JSON.parse(localStorage.getItem('currentUserShift'));
  }

  async insertUserCurrentShiftToPendingShifts(currentShift) {
    const pendingShifts = JSON.parse(localStorage.getItem('pendingShifts'));
    pendingShifts.push(currentShift)
    localStorage.setItem('pendingShifts', JSON.stringify(pendingShifts));
    this.updateLocalCurrentUserShift({ periods: {} });
    await this.insertPendingShiftsToFirebase();
    // localStorage.setItem('currentUserShift', JSON.stringify({}));
  }

  async insertPendingShiftsToFirebase() {
    const pendingShifts = JSON.parse(localStorage.getItem('pendingShifts'));
    console.log('pendingShifts:', pendingShifts);
    const failedToBeSentPendingShifts = [];
    for (let i = 0 ; i < pendingShifts.length ; i++) {
      try {
        const key = await this.insertUserCurrentShifttoHistory(pendingShifts[i]);
        console.log('key' , key);
      } catch (err) {
        console.log('Error while inserting into firebase', JSON.stringify(err));
        failedToBeSentPendingShifts.push(pendingShifts[i]);
      }
    }
    localStorage.setItem('pendingShifts', JSON.stringify(failedToBeSentPendingShifts));
    return true;
  }

  createFirebaseId() {
    return firebase.database().ref('/dummy').push().key;
  }

  getProducts() {
    return this.http.get(`${this.databaseUrl}/products/.json${this.authToken?('?auth='+this.authToken):''}`);
  }

  getOrganizations() {
    return this.http.get(`${this.databaseUrl}/organizations/.json${this.authToken?('?auth='+this.authToken):''}`);
  }

  insertUser(data) {
    return this.http.put(`${this.databaseUrl}/users/${data.uId}.json${this.authToken?('?auth='+this.authToken):''}`, data);
  }

  // an API to insert product views with firebase generated key
  insertProductViewAnalaytics(data) {
    return this.http.post(`${this.databaseUrl}/users/${this.userId}/product_views.json${this.authToken?('?auth='+this.authToken):''}`, data);
  }

  getProductViewsForUser() {
    return this.http.get(`${this.databaseUrl}/users/${this.userId}/product_views/.json${this.authToken?('?auth='+this.authToken):''}`);
  }

  getUserOrganization() {
    return this.http.get(`${this.databaseUrl}/users/${this.userId}/organization/.json${this.authToken?('?auth='+this.authToken):''}`);
  }

  fetchUserName() {
    return this.http.get(`${this.databaseUrl}/users/${this.userId}/userName/.json${this.authToken?('?auth='+this.authToken):''}`);
  }

  // updateUserName(data) {
  //   return this.http.patch(`${this.databaseUrl}/users/${this.userId}/.json${this.authToken?('?auth='+this.authToken):''}`, data);
  // }

  // an API to insert product views with custom timestamp based key
  insertProductViewAnalayticsTimestamp(data) {
    return this.http.put(`${this.databaseUrl}/product_views_timestamp/${data.scannedAt}.json${this.authToken?('?auth='+this.authToken):''}`, data);
  }

  // insertUserCurrentShift(data) {
  //   return this.http.patch(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`, data);
  // }

  // updateUserCurrentShift(data) {
  //   return this.http.patch(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`, data);
  // }

  // getUserCurrentShiftStartTime() {
  //   return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift/currentPeriodStartedAt.json${this.authToken?('?auth='+this.authToken):''}`);
  // }

  // getUserCurrentShiftStatus() {
  //   return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift/status.json${this.authToken?('?auth='+this.authToken):''}`);
  // }

  // insertUserPeriods(data) {
  //   return this.http.post(`${this.databaseUrl}/users/${this.userId}/currentShift/periods.json${this.authToken?('?auth='+this.authToken):''}`, data);
  // }

  // getUserCurrentShift() {
  //   return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`);
  // }

  getPendingShifts() {
    return JSON.parse(localStorage.getItem('pendingShifts'));
  }

  async insertUserCurrentShifttoHistory(data) {
    data.userId = this.userId;
    // return firebase.database().ref('/shiftHistory').push(data).key;
    if (data.startedAt && data.endedAt) {
      return this.http.post(`${this.databaseUrl}/shiftHistory/.json${this.authToken ? ('?auth=' + this.authToken) : ''}`, data).toPromise();
    } else {
      console.log('invalid data found, not inserting into firebase', JSON.stringify(data));
      return null;
    }
  }

  // removeUserCurrentShift(){
  //   return this.http.delete(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`);
  // }

  getUserWorkTimeHistory() {
    return this.http.get(`${this.databaseUrl}/shiftHistory.json${this.authToken?('?auth='+this.authToken):''}&orderBy="userId"&equalTo="${this.userId}"`);
  }

  insertUserCurrentLocation(data) {
    return this.http.post(`${this.databaseUrl}/users/${this.userId}/locationHistory.json${this.authToken?('?auth='+this.authToken):''}`, data);
  }

}

Environment.Prod.ts

export const environment = {
  production: true
};

Environment.TS

export const environment = {
  production: false,
  firebase: {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
  }
};

共有1个答案

曹浩波
2023-03-14

您正在此处导入environment.ts:

import { environment  } from '../environments/environment';

其中包含以下内容:

export const environment = {
  production: false,
  firebase: {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
  }
};
 类似资料:
  • 不过,当我尝试使用firebase数据库时,我做了一个const来获取firebase数据库中的现有房间。get()在线:const roomRef=await database。ref()。get(); 它报告以下错误:D:/react/app/letmeask/src/pages/Home中的TypeScript错误。tsx(35,65):类型“Reference”上不存在属性“get”。TS

  • 问题内容: 我在下面的代码块上收到此错误。 我还发现了表明存在功能的链接。 http://definitelytyped.org/docs/typescript-services-- typescriptServices/classes/typescript.stringutilities.html 我会错过一些文件吗? 问题答案: 是ES6函数,因此您需要以TypeScript编译器设置为目标,

  • 问题内容: 我有一个非常基本的与tsx交互的程序,我收到一个错误,我无法弄清为什么 git 将此代码克隆 问题答案: 拉下您的存储库并进行检查后,我意识到您没有打字稿的反应类型。 键入是管理和安装TypeScript定义的简单方法 添加这行 并再次运行一次即可解决该问题。尝试一下,让我知道这是否可以解决您的问题:) PS:TypeScript要求您描述对象和数据的形状。如果您查看我之前提供的其他答

  • 我正在遵循这个视频教程(文本版本相同)。我遵循了完全相同的代码,并收到了以下错误: 错误TS2339:属性“Get Employees”在类型“EmployeeService”上不存在

  • 我尝试开始使用TypeScript 2.4.1在我的旧AngularJS项目。首先,我试图重构我的简单控制器ts。在控制器上,我在编译过程中使用匿名函数获取

  • 新package.json: 当我使用新的package.json updates运行,然后运行时,会出现许多编译错误: src/app/all-matches/all-matches.component.ts(35,39)中的错误:错误TS2339:类型“Observable”上不存在属性“take tile”。src/app/app.component.ts(28,39):错误TS2339:类