我在向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,所以这不应该发生吗?有什么办法解决这个问题而不改变像字符串那样的数据结构?
答案 0 :(得分:1)
您无法将null
传递给set
命令。
set
将创建一个新对象。它不用于更新现有对象。
因此不需要null,因为在创建时会忽略它。您应该从对象中删除numero
和genero
。
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,了解有关set
和update
之间差异的更多信息。