gpt4 book ai didi

angular - 如何在 Firebase onSnapshot 中使用 .catch

转载 作者:行者123 更新时间:2023-12-03 22:54:50 24 4
gpt4 key购买 nike

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

  ngOnInit() {
this.messageService.getAllMessages()
.doc(`${this.userId[0] + '-' + this.userId[1]}`)
.collection('message')
.orderBy('createdAt', 'asc')
.onSnapshot((doc) => {
this.messages = [];
doc.forEach((snap) => {
this.messages.push({
content: snap.data().content,
createdAt: snap.data().createdAt,
userId: snap.data().userId
});
});
console.log('messages', this.messages);
});
}


问题是,如果在第一次尝试发送消息时没有消息,我第一次尝试访问该页面时不会加载该页面。

该消息有点模糊,但我很确定我遇到了这个问题,因为 Firebase 没有返回任何数据,但无法在查询中添加 .catch 来捕获错误并处理它,以便用户可以仍然导航到消息页面。

ERROR Error: Uncaught (in promise): Error: No value accessor for form control with unspecified name attribute
Error: No value accessor for form control with unspecified name attribute
at _throwError (vendor.js:70776)
at setUpControl (vendor.js:70646)
at NgModel._setUpStandalone (vendor.js:73693)
at NgModel._setUpControl (vendor.js:73668)
at NgModel.ngOnChanges (vendor.js:73604)
at checkAndUpdateDirectiveInline (vendor.js:63813)
at checkAndUpdateNodeInline (vendor.js:65493)
at checkAndUpdateNode (vendor.js:65432)
at debugCheckAndUpdateNode (vendor.js:66400)
at debugCheckDirectivesFn (vendor.js:66343)
at resolvePromise (polyfills.js:3193)
at resolvePromise (polyfills.js:3150)
at polyfills.js:3254
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2785)
at Object.onInvokeTask (vendor.js:57358)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2784)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2557)
at drainMicroTaskQueue (polyfills.js:2963)
defaultErrorLogger @ vendor.js:55398
handleError @ vendor.js:55450
next @ vendor.js:57952
schedulerFn @ vendor.js:52576
__tryOrUnsub @ vendor.js:133918
next @ vendor.js:133857
_next @ vendor.js:133804
next @ vendor.js:133781
next @ vendor.js:133566
emit @ vendor.js:52556
(anonymous) @ vendor.js:57389
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ polyfills.js:2753
push../node_modules/zone.js/dist/zone.js.Zone.run @ polyfills.js:2512
runOutsideAngular @ vendor.js:57315
onHandleError @ vendor.js:57389
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.handleError @ polyfills.js:2757
push../node_modules/zone.js/dist/zone.js.Zone.runGuarded @ polyfills.js:2526
_loop_1 @ polyfills.js:3056
api.microtaskDrainDone @ polyfills.js:3065
drainMicroTaskQueue @ polyfills.js:2970
Promise.then (async)
scheduleMicroTask @ polyfills.js:2946
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ polyfills.js:2775
onScheduleTask @ polyfills.js:2663
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ polyfills.js:2766
push../node_modules/zone.js/dist/zone.js.Zone.scheduleTask @ polyfills.js:2600
push../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask @ polyfills.js:2620
scheduleResolveOrReject @ polyfills.js:3241
resolvePromise @ polyfills.js:3187
(anonymous) @ polyfills.js:3103
webpackJsonpCallback @ runtime.js:26
(anonymous) @ pages-messages-messages-module.js:1
vendor.js:116244 POST https://firestore.googleapis.com/google.firestore.v1.Firestore/Write/channe


所以我的问题是,有没有办法在 firebase 中处理 .Snapshot 查询中的空返回值?

我试过这个:

  ngOnInit() {
try {
this.messageService.getAllMessages()
.doc(`${this.users[0] + '-' + this.users[1]}`)
.collection('message')
.orderBy('createdAt', 'asc')
.onSnapshot((doc) => {
this.messages = [];
doc.forEach((snap) => {
this.messages.push({
content: snap.data().content,
createdAt: snap.data().createdAt,
userId: snap.data().userId
});
});
console.log('messages', this.messages);
});
} catch (error) {
console.log('Message page error', error);
}
}

最佳答案

虽然 Doug answer没错,你还需要捕获 onSnapshot 的错误当权限被拒绝
为此,请使用以下语法:

const unsubscribeMe = firestoreInstance
.collection('example')
.onSnapshot(
querySnapshot => {
if (querySnapshot.empty) {
return
}
const organizations = querySnapshot.docs.map(ref => ({
id: ref.id,
...ref.data(),
}))
},
error => {
console.log(error)
}
)
javascript reference here其中包含可能适合您需要的其他方法签名。

关于angular - 如何在 Firebase onSnapshot 中使用 .catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61198904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com