erik.hac
erik.hac

Reputation: 111

Firebase undefined error

I'm having problem with sending null data to Firebase and I'm getting this error message:

Runtime Error Reference.update failed: First argument contains undefined in property

I have:

   public save(url: string, params: any) {
        return this.af.database.ref('alunos').push().set(params);
    }

as params i'm sending:

    this.aluno = {
            bairro: '',
            celular: '',
            cep: '',
            cidade: '',
            cpf: '',
            dataNascimento: '',
            email: '',
            nome: '',
            numero: null,
            rg: '',
            rua: '',
            ativo: true,
            genero: null,
            telefone: '',
            uf: ''
        };

I already know that the Firebase does not accept undefined but i'm using null, so this shouldn't happen right ? Any way to resolve this without changing the data structure like to string?

Upvotes: 1

Views: 1479

Answers (1)

sketchthat
sketchthat

Reputation: 2688

You can't pass null to a set command.

set will create a new object. It's not used for updating an existing object.

Therefore null isn't needed, as it's ignored when creating. You should remove numero and genero from your object.

this.aluno = {
  bairro: '',
  celular: '',
  cep: '',
  cidade: '',
  cpf: '',
  dataNascimento: '',
  email: '',
  nome: '',
  rg: '',
  rua: '',
  ativo: true,
  telefone: '',
  uf: ''
};

If you want to update an existing object then null is accepted when you make the command this.af.database.ref('alunos').update(params);

Have a read of the docs for more information on the differences between set and update.

Upvotes: 1

Related Questions