1

I am able to show all data from my products record in Firebase DB, what i want now is to delete a certain record that i select.

I got a bit of the documentation of Firebase but i'm not so good with VueJs, although i think this is more of a JavaScript problem.

What i get on my console when i click the delete link is:

Error: Firebase.child failed: First argument was an invalid path: "undefined". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

Here is my code:

<template>
<tbody v-for="(key, value, index) in products">
    <tr v-for="k, val in key">
        <td>{{ k.name }}</td>
        <td>{{ k.description }}</td>
        <td>{{ k.oldPrice }}</td>
        <td>{{ k.price }}</td>
        <td>{{ k.prodId }}</td>
        <td>{{ k.sellerName }}</td>
        <td><span class="glyphicon glyphicon-trash btn-delete-style" v-on:click="removeProduct(k)" title="Delete Product"></span></td>
    </tr>
</tbody>
</template>

<script>
removeProduct: function (product) {
  console.log("Product:" + product);
  productsRef.child(product['.key']).remove();
  toastr.success("Product deleted successfully");
}
</script>

Below you can see my DB:

enter image description here Any help is appreciated, Thank you.

5
  • what value do you get in product['.key'] Commented Feb 17, 2018 at 13:29
  • I get undefined Commented Feb 17, 2018 at 13:34
  • I cannot get the record with that key Commented Feb 17, 2018 at 13:43
  • you should use $key Commented Feb 17, 2018 at 13:56
  • dont know much about vue.js, but looks like you are passing a string to product[] that is y it is undefined. So first try to make sure that this productsRef.child(product['.key']).remove(); actually works, by passing in a hardcoded key. Then if that works, figure out how to get the key and pass it to the same expresion. Commented Feb 17, 2018 at 14:16

2 Answers 2

1

You need to reference first to that record and then call the remove function, here is an example:

<template>
  <tbody v-for="(key, value, index) in products">
      <tr v-for="k, val in key">
          <td>{{ k.name }}</td>
          <td>{{ k.description }}</td>
          <td>{{ k.oldPrice }}</td>
          <td>{{ k.price }}</td>
          <td>{{ k.prodId }}</td>
          <td>{{ k.sellerName }}</td>
          <td><span class="glyphicon glyphicon-trash btn-delete-style" v-on:click="removeProduct(k, k.sellerId, k.prodId)" title="Delete Product"></span></td>
      </tr>
  </tbody>
</template>
<script>
  removeProduct: function (product, sellerID, prodId) {
    var currentRef = db.ref('products/' + sellerID + '/' + prodId);
    currentRef.remove();
    toastr.success("Product deleted successfully");
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

This is it, Thank you
0

I think you should take product id from prodid

productsRef.child(product['prodid']).remove()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.