【问题标题】:how to insert data to specific user and access their own data in firebase?如何向特定用户插入数据并在firebase中访问他们自己的数据?
【发布时间】:2016-10-18 15:28:16
【问题描述】:

我是 Girebase 的新手。在我的 Ionic2 应用程序中,我正在尝试这样做:

当用户使用 Facebook 登录时,它会获取他的数据并获取 ID 并将其放入 Firebase。然后我想添加到特定的ID,他补充说。 我成功完成了所有这些操作,但是当我从用户注销时,我再次登录并删除了所有数据(他的所有笔记)。

我明白为什么会发生这种情况,因为每次我使用用户登录时,它都会再次调用 Firebase 以查看 ID、姓名和照片,然后它只是删除了之前的详细信息。

好的,这是我的 Firebase 结构

这是我的 Facebook 登录代码(可以正常使用)

   facebookLogin(){

    Facebook.login(['email']).then( (response) => {
      let facebookCredential = firebase.auth.FacebookAuthProvider
          .credential(response.authResponse.accessToken);
      var that = this;
      firebase.auth().signInWithCredential(facebookCredential)
          .then((success) => {
            console.log("Firebase success: " + JSON.stringify(success));  
            that.nav.setRoot(HomePage);
          })
          .catch((error) => {
            console.log("Firebase failure: " + JSON.stringify(error));
          });

    }).catch((error) => { console.log(error) });
  }

这是 home.ts (只是在前面解释一下:每次主页调用所以它会再次设置数据配置文件 - 可能是因为这个问题)

import { Component } from '@angular/core';

import { NavController,NavParams,LoadingController,AlertController,ViewController  } from 'ionic-angular';
import { Facebook } from 'ionic-native';

//import pages
import {LoginPage} from "../../pages/login/login";
import {User} from '../../models/user'

//import provider
import { ProfileData } from '../../providers/profile-data';
import { NotesData } from '../../providers/notes-data';

import firebase from 'firebase'
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {
  //facebook user
  userProfile: any = null;
  uid: any = null;
  fireUid:any=null;
  name:string=null;
  photo: any = null;
  user:User=null;

  //notes list

  notes:any=null;
  data:any;



  constructor(public navCtrl: NavController, public navParams: NavParams,public profileData:ProfileData,private viewCtrl: ViewController,public notesData:NotesData,private loadingCtrl: LoadingController,private alertCtrl: AlertController) {
    this.data={};
    this.data.title="";
    this.data.desc="";
  }
  ionViewDidLoad() {
    this.fireUid=firebase.auth().currentUser.uid;
    this.getDetailsFacebook();
    this.getNotesList();
  }

//facebook functions
  getDetailsFacebook() {
    var that=this;
    Facebook.getLoginStatus().then((response)=> {
      if (response.status == 'connected') {
        Facebook.api('/' + response.authResponse.userID + '?fields=id,name,gender', []).then((response)=> {
          //alert(JSON.stringify(response));
         that.uid = response.id;
          that.name=response.name;
         that.photo = "http://graph.facebook.com/"+that.uid+"/picture?type=large";
          that.user=new User(that.uid,that.fireUid,that.name, that.photo);
          that.profileData.setProfileData(that.user); // to create class for that

          //that.profileData.setProfile(that.uid,that.name,that.photo);
          //console.log("id:"+this.uid+this.name+this.photo);
        }, (error)=> {
          alert(error);
        })
      }
      else {
        alert('Not Logged in');
      }
    })


  }
  getPhoto() {
    var that=this;
    Facebook.getLoginStatus().then((response)=> {
      if (response.status == 'connected') {
        Facebook.api('/me', []).then((response)=> {
          alert(JSON.stringify(response));
          that.photo = "http://graph.facebook.com/"+response.that.uid+"/picture";
        }, (error)=> {
          alert(error);
        })
      }
      else {
        alert('Not Logged in');
      }
    })


  }
  logOutFacebook(){
    Facebook.logout().then((response)=>
    {
      this.navCtrl.push(LoginPage);
      alert(JSON.stringify(response));
    },(error)=>{
      alert(error);
    })
  }

  //notes functions
  getNotesList(){
    console.log("get event");
    var that=this;
    this.notesData.getNotesLIst().on('value', snapshot => {
      let notesList= [];
      snapshot.forEach( snap => {
        console.log("id note"+snap.val().id);
        notesList.push({
          id: snap.val().id,
          title: snap.val().title,
          desc: snap.val().desc,
        });
      });
      that.notes = notesList;
    });
  }
  addNotes() {
    //add preloader

    console.log(this.data.title,this.data.desc);
    this.notesData.createNote(this.data.title, this.data.desc);

    }

  delete(id:number){
  }
  update(id:number){}
}

那是我的个人资料数据提供者

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import firebase from 'firebase'

//import pages
import {User} from '../models/user'
/*
  Generated class for the ProfileData provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/

@Injectable()
export class ProfileData {

  public userProfile: any;
  public currentUser: any;

  constructor() {
    this.currentUser = firebase.auth().currentUser; // We'll use this to create a database reference to the userProfile node.
    this.userProfile = firebase.database().ref('/users'); // We'll use this to create an auth reference to the current user.
  }
  setProfileData(userObj:User){
    this.userProfile.child(userObj.fireUid).set({
      firstname:userObj.full_name,
      photoURl:userObj.photo
    });
  }

【问题讨论】:

    标签: firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您必须在“用户”之外创建“注释”。您可以将用户 ID 放在“notes”中,这样​​您就可以保存此信息。

    因为“用户”只保存用户名、id 和 phototuri。

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 2020-09-12
      • 1970-01-01
      • 2020-10-04
      • 2016-07-23
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多