【问题标题】:Can't perform Really Simple Firebase Database update using Angular 4 and Firebase 2无法使用 Angular 4 和 Firebase 2 执行真正简单的 Firebase 数据库更新
【发布时间】:2017-06-30 07:15:27
【问题描述】:

我正在构建的网络应用程序非常简单,并且使用 Angular 4 和 firebase2。它列出了一个包含歌曲的表格(标题、艺术家、

我在 Firebase 上创建了一个包含歌曲列表的对象/数组,每个对象/数组都是具有上述 3 个属性的对象。我正在努力做到这一点,当用户点击歌曲的核心时,喜欢的数量会增加一个,但我尝试过的功能似乎都不起作用。下面是我对这个 addLike 函数的尝试,以及产生的错误,下面是我的 html 模板、组件和数据结构的完整代码。任何帮助,将不胜感激。谢谢!

addLike(index){
this.songs.update(index, { likes: this.songs[index] + 1 });

} 

//(23,1): Supplied parameters do not match any signature of call target.

addLike(index){
this.songs[index].update({likes: this.songs[index] + 1 });
} 
//ERROR TypeError: Cannot read property 'update' of undefined

这是完整的代码

//COMPONENT HTML
<div> TEST </div>
<table class="table">
    <thead>
      <tr>
        <th>Title</th>
        <th>Artist</th>
        <th>Likes</th>
      </tr>
    </thead>
    <tbody>
  <tr *ngFor="let song of songs | async ; let i  = index">
<td>{{ song.title }}</td>
<td>{{ song.artist }}</td>
<td>{{ song.likes }}
 
            <i class="fa fa-heart-o" aria-hidden="true"  *ngIf="song.likes < 1"></i>
         <i class="fa fa-heart" aria-hidden="true" *ngIf="song.likes >= 1"></i>
<i class="fa fa-plus" aria-hidden="true" (click)="addLike(i)" ></i>

</td>
  </tr>
  </tbody>
  </table>

//COMPONENT TS

import { Component } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuthModule,AngularFireAuth} from 'angularfire2/auth';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})


export class AppComponent {
    title = 'Oxcord';


  songs: FirebaseObjectObservable<any>;
   constructor(db: AngularFireDatabase) {
    this.songs = db.object('/songs');
  }
  
  addLike(index){
this.songs[index].update({likes: this.songs[index] + 1 });

} 



}

{
  "songs" : [ {
    "artist" : "J Cole",
    "likes" : 3,
    "title" : "No Role Modelz"
  }, {
    "artist" : "Michael Jackson",
    "likes" : 8,
    "title" : "Thriller"
  }, {
    "artist" : "Meek Mill",
    "likes" : 0,
    "title" : "Trash"
  }, {
    "artist" : "Kendrick",
    "likes" : 6,
    "title" : "Humble"
  }, {
    "artist" : "Missy",
    "likes" : 4,
    "title" : "Work It"
  } ]
}

【问题讨论】:

    标签: angular firebase firebase-realtime-database angularfire2


    【解决方案1】:

    您正在更新本地 songs 属性。你应该更新你的数据库:

    addLike(id: string, likes: number): void {
      this.db.object(`/songs/${id}`).update({ likes: likes + 1 });
    }
    

    这样,您可以在歌曲列表中调用addLike 方法,传递歌曲key 和当前的点赞数:

    <i class="fa fa-plus" aria-hidden="true" (click)="addLike(song.$key, song.likes)" ></i>
    

    然后,在您的方法中,您可以更新数据库中该歌曲位置的点赞数。

    有关详细信息,请参阅documentation

    【讨论】:

    • 感谢您的回答。我刚刚尝试了您的代码,现在出现以下错误: ...../oxcord/src/app/app.component.ts (25,8) 中的错误:类型上不存在属性“db” '应用组件'。 webpack:编译失败。我错过了什么吗?
    • 您是否在构造函数中注入dbconstructor(private db: AngularFireDatabase) {}。请问你能创建一个 Plunker 吗?
    • 我的代码和上面完全一样,有 db 但没有“私有”,它似乎工作正常(当然除了下面的更新 fn)歌曲:FirebaseObjectObservable;构造函数(db: AngularFireDatabase) { this.songs = db.object('/songs');但是,是的,如果它有帮助的话,我可以做一个 plunker。我很快就会上班,然后我会去做
    • 抱歉,我很难弄清楚如何在 plunkr 上进行这项工作。 (老实说,我对一般编程比较陌生)这里是我的 git 存储库中的所有文件。 git repository here谢谢你的帮助!
    • 只是一个更新:我尝试使用 FirebaseListObservable 代替,在导出类 AppCompnent 中使用您的函数:addLike(id: string, likes: number): void { this.db.list(/songs/${id}) .update({ 喜欢:喜欢 +1 }); } // 我得到的错误是:提供的参数与调用目标的任何签名都不匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2017-05-24
    相关资源
    最近更新 更多