简体   繁体   English

Ionic,FireBase:从收藏夹列表中删除收藏夹

[英]Ionic, FireBase: Deleting favourites from a list of favourites

I am trying to solve this current code of mine which allows user to add favourites to a list and delete them.我正在尝试解决我的当前代码,它允许用户将收藏夹添加到列表中并删除它们。 Adding them is not an issue but deleting individually is a headache.添加它们不是问题,但单独删除是一件令人头疼的事情。 I am not sure what is wrong with my code.我不确定我的代码有什么问题。

favourites.page.ts收藏夹.page.ts

delete(item_key: any) {
    var userId = firebase.auth().currentUser.uid;
    console.log(item_key);
    return firebase.database().ref('fav/' + userId).child(item_key).remove();

  }

favourites.page.html收藏夹.page.html

<ion-row>
    <ion-col>
      <ion-card class="favCard" *ngFor="let item of fav; let i = index">
        <img (click)="direct(item.name)" src="{{ item.photo }}">
        <div class="info">
          <h2 class="name">{{ item.name }}</h2>
          <h3 class="cuisine">{{ item.cuisine }}</h3>
          <ion-button class="delete" (click)="delete(item.name)">Delete</ion-button>
        </div>
      </ion-card>

    </ion-col>
  </ion-row>

Firebase Database: Firebase 数据库:

Firebase 数据库映像

{
  "fav" : {
    "Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2" : {
      "beehoon" : {
        "country" : "Influenced by China",
        "cuisine" : "Chinese",
        "history" : "Rice vermicelli are a part of several Asian cuisines, where they are often eaten as part of a soup dish, stir-fry or salad. It also widely known in Asia by cognates of Hokkien 米粉(pronounced as bi-hun).",
        "name" : "Bee Hoon",
        "photo" : "https://firebasestorage.googleapis.com/v0/b/fyp-ionic-ad9e9.appspot.com/o/BreakfastCards%2Fbeehoon.jpg?alt=media&token=cfda6997-d2fb-49c0-b89e-e90f0519546f",
        "varieties" : "Pair up with sunny side up, luncheon meat and some good savoury Sambal chili.",
        "what" : "Also known as rice vermicelli, it is part of the rice noodles family. A loaded plate of stir-fried bee hoon piled with items such as fish cakes, sunny side ups and many more is a breakfast sight familiar to many Singaporeans."
      }
    },
    "SPl5PRkl6sYgn7KsAqqs9LhlhwB2" : {
      "beehoon" : {
        "country" : "Influenced by China",
        "cuisine" : "Chinese",
        "history" : "Rice vermicelli are a part of several Asian cuisines, where they are often eaten as part of a soup dish, stir-fry or salad. It also widely known in Asia by cognates of Hokkien 米粉(pronounced as bi-hun).",
        "name" : "Bee Hoon",
        "photo" : "https://firebasestorage.googleapis.com/v0/b/fyp-ionic-ad9e9.appspot.com/o/BreakfastCards%2Fbeehoon.jpg?alt=media&token=cfda6997-d2fb-49c0-b89e-e90f0519546f",
        "varieties" : "Pair up with sunny side up, luncheon meat and some good savoury Sambal chili.",
        "what" : "Also known as rice vermicelli, it is part of the rice noodles family. A loaded plate of stir-fried bee hoon piled with items such as fish cakes, sunny side ups and many more is a breakfast sight familiar to many Singaporeans."
      },
      "toast" : {
        "country" : "Influenced by China, Malaysia",
        "history" : "It was believed that Kaya toast originated with Hainanese people who worked on British ships as cooks. Eventually, these cooks settled in Singapore and started selling their food to the  locals. Wanting to create some uniqueness, the locals replaced the British jams with local coconut spread.",
        "name" : "Kaya Toast",
        "photo" : "https://firebasestorage.googleapis.com/v0/b/fyp-ionic-ad9e9.appspot.com/o/BreakfastCards%2Ftoast.jpg?alt=media&token=73e1a793-3002-4db2-97fb-4e3271ec2e93",
        "varieties" : "Cracker kaya toast, steamed kaya toast, French kaya toast",
        "what" : "Kaya toast is a well-known breakfast snack in Singapore and Malaysia. Kaya toast is prepared with kaya (also known as coconut jam) and butter, generally served on toast. It is considered a breakfast staple and remains popular in Singapore. "
      }
    }
  }
}

In this image I am trying to delete 'beehoon', however whenever i press the delete button, it is not deleted from the list and database.在这张图片中,我试图删除“beehoon”,但是每当我按下删除按钮时,它都不会从列表和数据库中删除。 Thus, it is still in the favourite lists.因此,它仍然在收藏夹列表中。

Much help is appreciated.非常感谢您的帮助。 Thanks谢谢

To delete a node from the database you must call remove() on a reference to its exact path.要从数据库中删除节点,您必须在对其确切路径的引用上调用remove() So if you want to remove the beehoon node under the first child in your JSON, you must call remove on /fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/beehoon .因此,如果要删除beehoon中第一个子节点下的 beehoon 节点,则必须在/fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/beehoon上调用 remove。

When the user clicks to delete the item, you call delete(item.name) .当用户单击删除项目时,您调用delete(item.name) So in the first node in your JSON that would translate to Bee Hoon .因此,在您的 JSON 的第一个节点中,它将转换为Bee Hoon And then in the delete function that would become:然后在delete function 中会变成:

firebase.database().ref('fav/' + userId).child("Bee Hoon").remove();

But there is no node /fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/Bee Hoon , since the node is called /fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/beehoon但是没有节点/fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/Bee Hoon ,因为节点被称为/fav/Hp9xMZzRMDRZnbW3sg8Zhhj6OMz2/beehoon

I'm not sure how you populate the fav that is used in let item of fav , but you'll need to ensure that each item also contains the key of its node.我不确定您如何填充favlet item of fav ,但您需要确保每个项目还包含其节点的 You'd then inject that key into the HTML in the call to delete , like delete(item.$key) .然后,您将在delete调用中将该密钥注入 HTML ,例如delete(item.$key)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM