Firebase未定义错误

时间:2017-09-04 01:32:04

标签: typescript firebase firebase-realtime-database

我在向Firebase发送空数据时遇到问题,并且收到此错误消息:

  

运行时错误   Reference.update失败:第一个参数在属性中包含undefined

我有:

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

as params我发送:

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

我已经知道Firebase不接受未定义但我使用null,所以这不应该发生吗?有什么办法解决这个问题而不改变像字符串那样的数据结构?

1 个答案:

答案 0 :(得分:1)

您无法将null传递给set命令。

set将创建一个新对象。它不用于更新现有对象。

因此不需要null,因为在创建时会忽略它。您应该从对象中删除numerogenero

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

如果要更新现有对象,则在发出命令null时接受this.af.database.ref('alunos').update(params);

阅读docs,了解有关setupdate之间差异的更多信息。