1

here I add my service file code.I want to store data in firebase but I can't get data.I got my data in console but get some error & not push in firebase

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Contact } from './logic/contact';
@Injectable({
  providedIn: 'root'
})
export class DataService {
list: AngularFireList<any>;
createcontact:Contact = new Contact();
  constructor(private firebase:AngularFireDatabase) { } 
  insertdata(contact: Contact) {
// here I get my data in console
        console.log(contact);
        this.list.push({
          name: contact.name,
          email:contact.email,
          phoneno:contact.phoneno,
          notes:contact.notes,
          address:contact.address,
          relation:contact.relation 
        });
      }
  updatedata(contact: Contact) {
    this.list.update(contact._id,{
      name: contact.name,
      email: contact.email,
      phoneno: contact.phoneno,
      notes: contact.notes,
      address: contact.address,
      relation: contact.relation
    });
  }
  }

the error is like

ERROR TypeError: Cannot read property 'push' of undefined

2 Answers 2

3

That's not how you push data to firebase. Try the following:

    export class DataService {
    list: AngularFireList<any>;
    createcontact:Contact = new Contact();
      constructor(private firebase:AngularFireDatabase) { } 
      insertdata(contact: Contact) {
    // here I get my data in console
            console.log(contact);
            this.list = this.firebase.list('/lists');
            if (contact) {
             this.list.push({
              name: contact.name,
              email:contact.email,
              phoneno:contact.phoneno,
              notes:contact.notes,
              address:contact.address,
              relation:contact.relation 
            });
          }
        }



updatedata(contact: Contact) {
    if ( contact) {
            this.list.set(contact._id,{
              name: contact.name,
              email: contact.email,
              phoneno: contact.phoneno,
              notes: contact.notes,
              address: contact.address,
              relation: contact.relation
            });
          }
    }
 }
Sign up to request clarification or add additional context in comments.

5 Comments

[ts] Type 'ThenableReference' is not assignable to type 'AngularFireList<any>'. Property 'query' is missing in type 'ThenableReference'.
@PoojaPatel my bad I made a typo mistake check the edit
Reference.push failed: first argument contains undefined in property 'list.name'
That means your object contact is empty, so check its value
For anyone like me trying to work out the working difference between the question and answer code, @Melchia is defining which list in Firebase to add it to with this line: this.list = this.firebase.list('/lists');
1

Just in case someone else encounter this kind of problem, I solved my issue by doing the following:

Define your variable as:

list$: AngularFireList<any[]>;

Then, in the constructor you initialize it as:

this.list$ = firebase.list('/contact'); //your path api

after that, push can be as follows:

this.list$.push([whatever]);

Hope it helps.

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.