Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在firebase onSnapshot中使用.catch_Angular_Typescript_Firebase_Google Cloud Firestore_Ionic4 - Fatal编程技术网

Angular 如何在firebase onSnapshot中使用.catch

Angular 如何在firebase onSnapshot中使用.catch,angular,typescript,firebase,google-cloud-firestore,ionic4,Angular,Typescript,Firebase,Google Cloud Firestore,Ionic4,我已经创建了一个Ionic Firebase聊天应用程序。我想我遇到的问题是,我在初始化消息页面时设置了一个查询快照,如下所示: ngOnInit(){ this.messageService.getAllMessages() .doc(`${this.userId[0]+'-'+this.userId[1]}`) .collection('消息') .orderBy('createdAt','asc') .onSnapshot((文档)=>{ this.messages=[]; 文件格式((

我已经创建了一个Ionic Firebase聊天应用程序。我想我遇到的问题是,我在初始化消息页面时设置了一个查询快照,如下所示:

ngOnInit(){
this.messageService.getAllMessages()
.doc(`${this.userId[0]+'-'+this.userId[1]}`)
.collection('消息')
.orderBy('createdAt','asc')
.onSnapshot((文档)=>{
this.messages=[];
文件格式((快照)=>{
这个是.messages.push({
内容:snap.data().content,
createdAt:snap.data().createdAt,
userId:snap.data().userId
});
});
console.log('messages',this.messages);
});

}
如果没有找到匹配的文档,Firestore查询不会失败。它只提供一个不包含文档的QuerySnapshot

代码中的
doc
变量是一个类型对象(因此,它不是“文档”)。它有一个名为的方法,您可以使用该方法查看它是否包含任何文档。或者,您可以检查其零长度属性

.onSnapshot(querySnapshot=>{
如果(!querySnapshot.empty()){
this.messages=[];
querySnapshot.forEach((捕捉)=>{
这个是.messages.push({
内容:snap.data().content,
createdAt:snap.data().createdAt,
userId:snap.data().userId
});
});
}
否则{
//如果没有文档,您想做什么?
}
});
根据这些建议,我已经用以下内容更新了代码,并且出现了相同的问题:

ngOnInit(){
this.messageService.getAllMessages()
.doc(`${this.users[0]+'-'+this.users[1]}`)
.collection('消息')
.orderBy('createdAt','asc')
.onSnapshot((文档)=>{
如果(!单据为空){
this.messages=[];
文件格式((快照)=>{
这个是.messages.push({
内容:snap.data().content,
createdAt:snap.data().createdAt,
userId:snap.data().userId
});
});
}否则{
this.messages=[];
}
console.log('messages',this.messages);
});
}

{{msg.content}

{{msg.createdAt|date:'short'} {{msg.content}
{{msg.createdAt|date:'short'}
虽然是正确的,但是当权限被拒绝时,您仍然需要捕获
onSnapshot
的错误

为此,请使用以下语法:

const unsubscribeMe=firestoreInstance
.collection('示例')
onSnapshot先生(
querySnapshot=>{
if(querySnapshot.empty){
返回
}
const organizations=querySnapshot.docs.map(ref=>({
id:ref.id,
…参考数据(),
}))
},
错误=>{
console.log(错误)
}
)

请参阅包含可能适合您需要的附加方法签名的。

谢谢Doug。您的代码片段帮助我确定查询不是问题的根源,而是其他原因。我已经添加了我正在使用的html以及根据您的建议更新的代码。您能看到可能导致此问题的其他原因吗?请尝试对错误消息进行web搜索。“未指定名称属性的表单控件的无值访问器”我发现了…html的标签依赖于来自firebase的数据。我为属性添加了一个ngIf并解决了这个问题。回到您最初的问题:“如何处理快照上的错误”-有办法吗?我不相信您可以将捕获链接到它,我很困惑,因为它也没有告诉您哪个“onSnapshot”出现了错误,而且如果没有堆栈跟踪,调试工作会很困难。