Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Data to Firebase Cloud Firestore and Angular

First of all I have to say that I'm learning Angular. Maybe the answer is too evident but I can't see it. I've searched responses for similar questions in the web but I didn't found any that works for me.

I made an Angular Template Driven Form to store products on my Firebase Cloud Firestore Database. I created a model called "Product"

product.ts

export class Product {
constructor(
    public code: string,
    public desc: string,
    public stock: number,
    public price: number,
    public off: number,
    public details?: string
) {}

}

Then, I have the product.component.ts

import { Component } from '@angular/core';
import { Product } from './product';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';


@Component({
    selector: 'app-admin-product',
    templateUrl: 'product.component.html'
})

export class AdminProductComponent {

model = new Product('', '', 0, 0, 0);
successMsg = 'Data successfully saved.';

productsRef: AngularFirestoreCollection<Product>;
product: Observable<Product[]>;

constructor(private afs: AngularFirestore) {
    this.productsRef = this.afs.collection<Product>('productos');
}

save() {
    this.productsRef.add(this.model).then( _ => alert(this.successMsg));
}

TypeScript doesn't return errors at all and everything seems to be ok. But when I run my app and try to save the form's data, console returns next error:

AdminProductComponent.html:51 ERROR Error: Function CollectionReference.add() requires its first argument to be of type object, but it was: a custom Product object

I solved it passing the custom object to a simple object like this:

const product = {
    code: this.model.code,
    desc: this.model.desc,
    stock: this.model.stock,
    price: this.model.price,
    off: this.model.off,
    details: this.model.details
}

And saving data like this:

save() {
    this.productsRef.add(product).then( _ => alert(this.successMsg)); }

But I think that it isn't the properly solution, maybe could cause future issues scalling the app.

like image 683
Marcelo J Forclaz Avatar asked Nov 04 '17 12:11

Marcelo J Forclaz


3 Answers

I used typescript spread operator

  add(wizard: Wizard): Promise<DocumentReference> {
    return this.wizardCollection.add({...wizard});
  }
like image 101
peterbrown Avatar answered Sep 19 '22 17:09

peterbrown


You can try this way, hope you are using latest typescript

product.ts

export interface Product {
   code: string;
   desc: string;
   stock: number;
   price: number;
   off: number;
   details?: string
}

in your product.component.ts

export class AdminProductComponent {

model:Product;
successMsg = 'Data successfully saved.';

productsRef: AngularFirestoreCollection<Product>;
product: Observable<Product[]>;

constructor(private afs: AngularFirestore) {
    this.productsRef = this.afs.collection<Product>('productos');

    this.model = {
      code:'',
      desc:'',
      stock:0,
      price:0,
      off:0
    }
}

save() {
    this.productsRef.add(this.model).then( _ => alert(this.successMsg));
}

I think by doing this model = new Product('', '', 0, 0, 0); you get an instance of the class not the object.

like image 24
Hareesh Avatar answered Sep 19 '22 17:09

Hareesh


I used

save() {
    const param = JSON.parse(JSON.stringify(this.model));
    this.productsRef.add(param).then( _ => alert(this.successMsg));
 }
like image 45
Rodger Gutierrez Avatar answered Sep 22 '22 17:09

Rodger Gutierrez