如何引用Firebase pushId键?

时间:2018-10-21 07:07:53

标签: javascript firebase ionic-framework ionic4

我有以下代码,我试图在“ upvote”功能中使用firebase密钥,但是,它不允许我在“ upvote”中引用“ key”。我的问题是,由于其他功能(例如“编辑”和“异步删除”)都可以引用“密钥”,它们从何处获取以及如何在“ upvote”中使用它?

import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { Router, ActivatedRoute } from '@angular/router';
import * as firebase from 'Firebase';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
infos = [];
  ref = firebase.database().ref('infos/').orderByChild('vote');
  constructor(private route: ActivatedRoute, public router: Router, public alertController: AlertController) {
    this.ref.on('value', resp => {
      this.infos = [];
      this.infos = snapshotToArray(resp);  
    });
}
  edit(key) {
    this.router.navigate(['/edit/'+key]);
   console.log(key); <------ Correct Key-------
  }
  async delete(key) {
  console.log(key); <------ Correct Key-------
    const alert = await this.alertController.create({
      header: 'Confirm!',
      message: 'Are you sure want to delete this info?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('cancel');
          }
        }, {
          text: 'Okay',
          handler: () => {
            firebase.database().ref('infos/'+key).remove();
          }
        }
      ]
    });
    await alert.present();
    console.log(key);
  }
upvote(key) {
const data = { };
console.log(key);  <------ Undefined? -------
firebase.database().ref('infos/' + key +'/vote').transaction(function(value) {
  if (value) {
    value--;
  }
 return value;
});
}}
export const snapshotToArray = snapshot => {
  let returnArr = [];
  snapshot.forEach(childSnapshot => {
      let item = childSnapshot.val();
      item.key = childSnapshot.key;
      returnArr.push(item);
  });
  return returnArr;
}

编辑:通过修复如何在html中引用函数来解决我的问题。我只是没有正确地调用它。见下文:

<ion-item-sliding *ngFor="let info of infos" (click)="detail(info.key)">
      <ion-item detail lines="full" >
        <ion-icon name="desktop" slot="start"></ion-icon>
        {{info.info_title}} - {{info.vote2}} - {{info.key}}
          <ion-button shape="round" color="primary" expand="block" (click)="upvote(info.key)">Upvote</ion-button>
        <ion-button shape="round" color="primary" expand="block" (click)="downvote(info.key)">Downvote</ion-button>

      </ion-item>

      <ion-item-options side="end">
        <ion-item-option color="primary" (click)="edit(info.key)">EDIT</ion-item-option>
        <ion-item-option color="danger" (click)="delete(info.key)">DELETE</ion-item-option>
      </ion-item-options>
    </ion-item-sliding>

在我这样称呼“ upvote”之前: 投票

结果不确定,令人头疼!

0 个答案:

没有答案