如何从Firebase Realtime数据库中删除记录

时间:2019-07-13 12:14:28

标签: javascript firebase api firebase-realtime-database nuxt.js

我正在用Nuxt.js创建一个应用程序,该应用程序从API检索一些电影数据,并且登录后(通过Firebase身份验证),您可以在“收藏夹”部分中添加电影/电视连续剧。 我不明白如何插入控件以避免插入重复项(如果我已经在收藏夹中添加了“矩阵”,并尝试再次添加它,则不能添加它)以及如何删除某些记录。 我给他的逻辑是: -当用户单击以将项目添加到收藏夹时,将创建一个对象,该对象由Firebase创建的用户的ID标识,然后根据是电影还是电视连续剧将其插入子组。 / p>

用户单击添加: `

addToFavorites(item) {
      let tipo
      if (item.title) {
        tipo = 'movie'
      }
      if (item.name) {
        tipo = 'tv'
      }
      if (this.$store.state.token) {
        if (tipo === 'movie') {
          this.$store
            .dispatch('addToDatabase2', {
              title: item.title,
              type: 'movie',
              id: item.id,
              poster_path: item.poster_path
            })
            .then(() => {
              this.snackbar = true
            })
            .then(() => {
              console.log(
                'heroTrending.component-->addToFavorites(): Movie aggiunto con successo'
              )
              // this.$router.push('/user')
            })
        }
        if (tipo === 'tv') {
          this.$store
            .dispatch('addToDatabase2', {
              title: item.name,
              type: 'tv',
              id: item.id,
              poster_path: item.poster_path
            })
            .then(() => {
              this.snackbar = true
            })
            .then(() => {
              console.log(
                'HeroTrending.component-->addToFavorites(): Serie aggiunta con successo'
              )
              // this.$router.push('/user')
            })
        }
      } else {
        this.$router.push('/user/auth')
      }
    }

`

然后在商店中: `

addToDatabase2(vuexContext, item) {
        if (item.type === 'movie') {
          return this.$axios
            .$post(
              'https://what-can-i-see-bf593.firebaseio.com/' +
                vuexContext.state.localId +
                '/MOVIES.json?auth=' +
                vuexContext.state.token,
              item
            )
            .then(() => {
              console.log('$store-->addToDatabase2(): item.type == movie')
            })
            .catch(e => console.log(e))
        }
        if (item.type === 'tv') {
          return this.$axios
            .$post(
              'https://what-can-i-see-bf593.firebaseio.com/' +
                vuexContext.state.localId +
                '/TVs.json?auth=' +
                vuexContext.state.token,
              item
            )
            .then(() => {
              console.log('$store-->addToDatabase2(): item.type == tv')
            })
            .catch(e => console.log(e))
        }
      },

`

当用户进入“收藏夹”部分时:

`

showMoviesFavorites() {
  this.$store.dispatch('mostraMoviePreferiti')
},
showSeriesFavorites() {
  this.$store.dispatch('mostraSeriePreferiti')
},

Then in the store:

mostraMoviePreferiti(vuexContext) {
        this.$axios
          .$get(
            'https://what-can-i-see-bf593.firebaseio.com/' +
              vuexContext.state.localId +
              '/MOVIES.json'
          )
          .then(res => {
            console.log(res)
            vuexContext.commit('addFavMovie', res)
          })
      },
      mostraSeriePreferiti(vuexContext) {
        this.$axios
          .$get(
            'https://what-can-i-see-bf593.firebaseio.com/' +
              vuexContext.state.localId +
              '/TVs.json'
          )
          .then(res => {
            console.log(res)
            vuexContext.commit('addFavSerie', res)
          })
      }
    },


      addFavMovie(state, item) {
        state.favoriteMovies = item
      },
      addFavSerie(state, item) {
        state.favoriteSeries = item
      },

`

firebase实时数据的结构为: `

{
  "mPulHwx4C6eTQEwUhhXXTsGb2eq2" : {
    "MOVIES" : {
      "-LjMnx_GdJt2qQNlh7ko" : {
        "id" : 238,
        "poster_path" : "/rPdtLWNsZmAtoZl9PK7S2wE3qiS.jpg",
        "title" : "The Godfather",
        "type" : "movie"
      }
    },
    "TVs" : {
      "-LjMo4XjVOlUVnvs_iiw" : {
        "id" : 60625,
        "poster_path" : "/qJdfO3ahgAMf2rcmhoqngjBBZW1.jpg",
        "title" : "Rick and Morty",
        "type" : "tv"
      }
    }
  }
}

`

如何删除特定项目? 如何插入支票以避免重复?

0 个答案:

没有答案