使用push(data)Angularfire2时出现处理错误 [英] Handeling Error when using push(data) Angularfire2
问题描述
使用angularFire2推送数据时如何处理错误?
How to handle Error when pushing data with angularFire2?
我使用以下方法:
todoLists: AngularFireList<TodoList>;
addList(data): ThenableReference {
const item: Item = {
name: data.task,
state: false,
description: 'No description'
};
const todoList: TodoList = {
id: '',
name: data.name,
items: [item]
};
this.todoLists.push(todoList).then(_ => this.presentToast('List succesfuly added'))
.catch(err => _ => this.presentToast('Something wrong happened'));
}
这里的问题是AngularFire
的push方法返回一个ThenableReference
,因此该接口中不存在catch方法.
The problem here is that the push method of AngularFire
returns an ThenableReference
so the catch method doesn't exist in that interface.
这是来自编辑器(vscode)的消息
Here's the message from the editor (vscode)
promiseLike类型的属性不存在<> 必须有另一种方法来处理错误.
property catch doesn't exist on type promiseLike<> There must another way to handle errors.
推荐答案
我遇到了同样的问题,并且发现可以用push()方法创建thenable引用,然后使用set方法在.catch上返回错误.
I had the same problem, and I found I could create the thenable reference with the push() method, then use set which returns an error on .catch.
在文档中的更多此处.
todoLists: AngularFireList<TodoList>;
addList(data): void {
const item: Item = {
name: data.task,
state: false,
description: 'No description'
};
const todoList: TodoList = {
id: '',
name: data.name,
items: [item]
};
// .push() also creates a new unique key, which can be accessed with ref.key
let ref: Reference = this.todoLists.push();
ref.set(todoList)
.then( () => this.presentToast('List succesfuly added'))
.catch(err => this.presentToast('Something wrong happened: ' + err));
}
这篇关于使用push(data)Angularfire2时出现处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!