【问题标题】:how can l read data form firebase我如何从 firebase 读取数据
【发布时间】:2019-01-17 17:38:46
【问题描述】:

我正在尝试通过 userId 将数据推送到 firebase 数据库,即使他可以修改的每个用户都有 post 或 information 。推送数据成功,但问题是当我尝试获取 html 中的数据时不显示。

数据库

html

获取数据的代码:

items: Observable<any[]>;
itemsRef: AngularFireList<any>;
  constructor(public fire: AngularFireAuth,public db: AngularFireDatabase) 
    {
        this.itemsRef = db.list(`report/`);
        // Use snapshotChanges().map() to store the key
        this.items = this.itemsRef.snapshotChanges().pipe(
          map(changes => 
            changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
          )

        );

      }

html

 <ion-list *ngFor="let item of items | async"> 
    <ion-item-sliding>
      <ion-item>
        <h2>{{itemstitle}}</h2>
        <p>{{item.name}}</p>
        <p>{{item.username}}</p>
        <p>{{item.dateadded}}</p>
    </ion-item>

      <ion-item-options side="right"> 
        <button ion-button color="danger" (click)="deletReport(item.key)">
          <ion-icon  ios="ios-trash" md="md-trash" item-end large></ion-icon>
        </button>
        <button ion-button color="primary" (click)="updatereport(item.key,item.name,item.title)">
          <ion-icon ios="ios-create" md="md-create"></ion-icon>
        </button>
      </ion-item-options>

    </ion-item-sliding>
  </ion-list>

我的推送数据:

name :string='';
  title :string='';
  Email:string='';
  dateadded:string='';
  userId: string;

  reports: AngularFireList<any>;
  items: Observable<any[]>;

  constructor(db: AngularFireDatabase,public alertCtrl: AlertController,public loadingCtrl: LoadingController,
    public navCtrl: NavController,public fire: AngularFireAuth) {
     var newPostKey = db.database.ref().child('report').push().key;
    this.reports = db.list(`report/${this.userId=fire.auth.currentUser.uid}/`);

    console.log(this.userId=fire.auth.currentUser.uid)

  }

  formatDate (date): string{

    const day = new Date().getDate();
    const month = new Date().getMonth()+1;
    const year = new Date().getFullYear();
    return `${day}-${month}-${year}`
  }
// dateaddread(dateadded){
//   this.dateadded = this.formatDate(dateadded)
// }

  Publish(name,title,dateadded){
    if (name.trim().length === 0) {
      console.log(this.name);
      let alert = this.alertCtrl.create({
        title: 'Error',
        message: 'Please fill report blank ',
        buttons: ['OK']
    });
    alert.present(); 
    }else if (title.trim().length === 0){

      let alert = this.alertCtrl.create({
        title: 'Error',
        message: 'Please fill title blank ',
        buttons: ['OK']
    });
    alert.present(); 
    }else {

      this.reports.push({
        name:name,
        title:title,
        Email:this.fire.auth.currentUser.email,
        dateadded:this.formatDate(dateadded)

      }).then(Newreport=>{
        let alert = this.alertCtrl.create({
          title: 'Successful',
          message: 'Successfully posted',
          buttons: [
            {
              text: 'Ok',
              handler: () => {
            let navTransition = alert.dismiss();
            // start some async method
            navTransition.then(() => {
            this.navCtrl.pop().then(data => {
            this.navCtrl.setRoot(FeedPage)

            });
       });
      return false;
    }
    }]          
    });

      alert.present()

      })
    }

    }

任何想法请用简单的代码?

【问题讨论】:

    标签: firebase firebase-realtime-database ionic3


    【解决方案1】:
    this.reports = db.list(`report/${this.userId=fire.auth.currentUser.uid}/`);
    
    this.reports.push({
        name:name,
        title:title,
        Email:this.fire.auth.currentUser.email,
        dateadded:this.formatDate(dateadded)
    })
    

    为了在 HTML 中显示数据,您只需修改查询以仅获取登录用户的数据

    this.itemsRef = db.list(`report/` + fire.auth.currentUser.uid);
    

    【讨论】:

    • 感谢您的回答。但是用户如何修改只有自己的数据?! .因为根据您的回答,每个用户都可以修改其他人的数据。我希望只有他可以修改的用户有自己的数据!
    • push() 函数将数据列表作为数组推送,但在这里您只想为每个用户创建单个节点,因此您可能必须用 set 替换 push 函数,用对象替换 list 函数,我有编辑了我的答案,你可以检查一下,这里是 angular firebase github.com/angular/angularfire2/blob/master/docs/rtdb/… 的链接以获取更多信息
    • 非常感谢您的回复,我按照您说的做了。并且工作正常。但是如果我用set用户推送数据,他就不能推送其他数据! .他只需要更新或删除一项数据。我希望用户他推送很多数据不仅仅是一个数据,我的应用程序内容应用程序博客。我不知道你是否明白我的意思!
    • 那么您将不得不使用推送功能而不是集合,兄弟,当您从 Firebase 获取数据时,请告诉我一件事,您只显示当前登录用户已发布的 HTML 数据/edited 不是所有用户的所有数据,对吗?
    • 是的,我必须使用 push not set 。是的,你的权利。 l 仅获取当前登录用户已发布/编辑的 HTML 数据。甚至我尝试与另一个用户相同。
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多