0

I have developed simple shopping cart. i am getting and displayed selected items successfully in html but need to store selected items (created as array) to firebase Database. I don't know how can I write selected items to firebase, if anyone knows kindly help me out.

This is my array. enter image description here

cart.page.html

<form [formGroup]="createDataForm" (submit)="createData()">
<ion-list>

 <ion-grid><ion-row *ngFor="let item of selectedItems" lines="inset">
  <ion-col size="5"><div>{{ item.name }} - {{ item.price | currency:'USD':'symbol' }}</div></ion-col>
  <ion-col size="4.5">

  <ion-col size="2"><ion-label text-right> {{ (item.price * item.count) | currency:'USD':'symbol' }} 
   </ion-label></ion-col>
   </ion-row></ion-grid>
  </ion-list>

3
  • 1
    firebase realtime database or firestore? Commented Jun 13, 2020 at 11:53
  • Have you tried the answer? Commented Jun 13, 2020 at 17:43
  • I am working on Firestore, welcome if any solution available on it. Commented Jun 16, 2020 at 2:03

2 Answers 2

1

Try the following:

saveData(){
   let database = firebase.database();
   this.selectedItems.forEach((values) => {
     database.ref("shopping-cart").push(values);
   });
 }

https://firebase.google.com/docs/reference/js/firebase.database.Reference#push

Sign up to request clarification or add additional context in comments.

Comments

0

Finally, i got a solution. given below

Note: It worked on firestore.

savedata() {
// Add a new document in collection "cities"
var db = firebase.firestore();
var n = this.selectedItems;
db.collection("myList").doc("MA").set({
n
})
.then(function() {
  console.log("Document successfully written!");
})
.catch(function(error) {
  console.error("Error writing document: ", error);
});

}

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.