使用localStorage

时间:2018-04-03 19:47:47

标签: angular typescript firebase ionic-framework

我正在为一个高级设计项目开发一个项目,并且遇到了一些奇怪的typeScript错误。我正在使用打字稿2.3.4。

import { Component } from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth'
import { LoginPage } from '../login/login'
import { AngularFireDatabase, FirebaseObjectObservable} from "angularfire2/database-deprecated";
import * as firebase from 'firebase';
import { AlertController } from 'ionic-angular';
/**
 * Generated class for the Profile page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-profile',
  templateUrl: 'profile.html',
})
export class ProfilePage {
  userID: string;
  userProfile: FirebaseObjectObservable<any>;
  profiles: FirebaseObjectObservable<any>;
  public profile = {};
  public items: Array<any> = [];
  constructor(public navCtrl: NavController, private afAuth: AngularFireAuth,        public af: AngularFireDatabase) {
      this.afAuth.authState.subscribe(user => {
      if(user) this.userID = user.uid;
      this.userProfile = af.object(`userProfile/${user.uid}`);
      this.profiles = af.object(`profiles/${user.uid}/`);
    });
  }

    ionViewDidLoad() {
    console.log('ionViewDidLoad Profile');
    var tmp1='', tmp2='', tmp3='', tmp4='', tmp5 = '';
    const personRef: firebase.database.Reference =    firebase.database().ref(`profiles/${this.userID}`);
    const personRefCheck: firebase.database.Reference = firebase.database().ref(`profiles/${this.userID}`);
    console.log("personRefCheck: "+personRefCheck);
    personRefCheck.on('value', checkSnapshot => {
      if(!checkSnapshot.val()) {
        console.log("!checkSnapshot");
        //Sets the local variables on load if the profile is null.
        localStorage.setItem('Pname', tmp1);
        localStorage.setItem('Page', tmp2);
        localStorage.setItem('Pabout', tmp3);
        localStorage.setItem('Plocation', tmp4);
        localStorage.setItem('Prating', tmp5);
        }

      else {
        personRef.on('value', personSnapshot => {
          console.log("personalSnapshot");
          this.profiles = personSnapshot.val();
          console.log(this.profiles.name);
          //Sets the variables to the values from database when the page loads
          localStorage.setItem('Pname', this.profiles.name);
          localStorage.setItem('Page', this.profiles.age);
          localStorage.setItem('Pabout', this.profiles.about);
          localStorage.setItem('Plocation', this.profiles.location);
          localStorage.setItem('Prating', this.profiles.rating);
         });
      }
    });
  }

  submitProfileChanges(name, age, about, location, rating) {
     //theoretically should have a .then() promise
     //push a newTask to the 'tasks' database and set the owner and name
     var var1, var2, var3, var4, var5;
     //Variables set to local localStorage
     var1 = localStorage.getItem('Pname');
     var2 = localStorage.getItem('Page');
     var3 = localStorage.getItem('Pabout');
     var4 = localStorage.getItem('Plocation');
     var5 = localStorage.getItem('Prating');

     //If input is is null, sets the input equal to the database, then passes that back to the database
     if(name == undefined){
       name = var1;
     }
     if(age == undefined){
       age = var2;
     }
     if (about == undefined){
       about = var3;
     }
     if (location == undefined){
       location = var4;
     }
     if (rating == undefined){
       rating = var5;
     }
     //Pushes the profile update to the database
     var profUpdate = this.profiles.update({
       name,
       age,
       about,
       location,
       rating
    });
    alert("Profile Updated!");
  }

  logoutUser() {
     this.navCtrl.setRoot(LoginPage);
  }
}

这是导致问题的代码部分。以下是我在提供此未注释时所获得的错误。 “FirebaseObjectObservable”类型中不存在“名称” 我收到有关年龄,地址,位置和评分的错误。

localStorage.setItem('Pname', this.profiles.name);
localStorage.setItem('Page', this.profiles.age);
localStorage.setItem('Pabout', this.profiles.about);
localStorage.setItem('Plocation', this.profiles.location);
localStorage.setItem('Prating', this.profiles.rating);

如果我对上面的代码片段进行评论,程序将在终端行中使用离子服务进行编译。 继发号之后,我可以对已经注释掉的代码发表评论,该程序将自行重建并完美运行。

运行离子信息会吐出以下内容:

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

global packages:

    cordova (Cordova CLI) : 8.0.0 

local packages:

    @ionic/app-scripts : 3.1.8
    Cordova Platforms  : ios 4.5.4
    Ionic Framework    : ionic-angular 3.9.2

System:

    Node  : v9.9.0
    npm   : 4.6.1 
    OS    : macOS High Sierra
    Xcode : Xcode 9.2 Build version 9C40b 

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro    

1 个答案:

答案 0 :(得分:2)

错误消息告诉您应该知道的所有内容。你提前迈出了一步。

personSnapshot是一个FirebaseObjectObservable,其中包含您要访问的对象。但要获得此对象,您必须首先订阅FirebaseObjectObservable!

以这种方式尝试:

else {
  personRef.on('value', personSnapshot => {
    console.log("personalSnapshot");

     personSnapshot.subscribe(dbObject => {

        console.log(dbObject.name);
        //Sets the variables to the values from database when the page loads
        localStorage.setItem('Pname', dbObject.name);
        localStorage.setItem('Page', dbObject.age);
        localStorage.setItem('Pabout', dbObject.about);
        localStorage.setItem('Plocation', dbObject.location);
        localStorage.setItem('Prating', dbObject.rating);

     });

  });
}