Title: idField on valueChanges doesn't work as expected. · Issue #2753 · angular/angularfire · GitHub
Open Graph Title: idField on valueChanges doesn't work as expected. · Issue #2753 · angular/angularfire
X Title: idField on valueChanges doesn't work as expected. · Issue #2753 · angular/angularfire
Description: Version info Angular: 11 Firebase: 8 AngularFire: 6 Other: MacOS Big Sur Issue When you call valueChanges on a document you know doesn't exist, it emits undefined. However if you specify idField, for example, as id, then it will return a...
Open Graph Description: Version info Angular: 11 Firebase: 8 AngularFire: 6 Other: MacOS Big Sur Issue When you call valueChanges on a document you know doesn't exist, it emits undefined. However if you specify idField, f...
X Description: Version info Angular: 11 Firebase: 8 AngularFire: 6 Other: MacOS Big Sur Issue When you call valueChanges on a document you know doesn't exist, it emits undefined. However if you specify idFiel...
Opengraph URL: https://github.com/angular/angularfire/issues/2753
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"idField on valueChanges doesn't work as expected.","articleBody":"### Version info\r\n\r\n**Angular: 11**\r\n\r\n**Firebase: 8**\r\n\r\n**AngularFire: 6**\r\n\r\n**Other: MacOS Big Sur**\r\n\r\n### Issue\r\n\r\nWhen you call valueChanges on a document you know doesn't exist, it emits undefined. However if you specify idField, for example, as id, then it will return an object with the field id. I expected that it would still return undefined. It returning causes problems, as the object then isn't in the shape you expect (if you specify a type of the result) but also you get a result when a document doesn't actually exist.\r\n\r\nI think the issue is simply in the valueChanges function:\r\n\r\n```\r\nvalueChanges\u003cK extends string\u003e(options: { idField?: K } = {}): Observable\u003cT | undefined\u003e {\r\n return this.snapshotChanges().pipe(\r\n map(({ payload }) =\u003e\r\n options.idField ? {\r\n ...payload.data(),\r\n ...{ [options.idField]: payload.id }\r\n } as T \u0026 { [T in K]: string } : payload.data()\r\n )\r\n );\r\n}\r\n```\r\n\r\nAs you can see, if payload.data() is undefined, this function will create an object with id field. I think a simple fix would be:\r\n\r\n```\r\nvalueChanges\u003cK extends string\u003e(options: { idField?: K } = {}): Observable\u003cT | undefined\u003e {\r\n return this.snapshotChanges().pipe(\r\n map(({ payload }) =\u003e\r\n options.idField \u0026\u0026 payload.data() ? {\r\n ...payload.data(),\r\n ...{ [options.idField]: payload.id }\r\n } as T \u0026 { [T in K]: string } : payload.data()\r\n )\r\n );\r\n}\r\n```\r\n\r\nI don't actually know much about the underlying firebase apis, but I'm assuming that payload.data() is undefined if there is no document.","author":{"url":"https://github.com/ammaarpatel99","@type":"Person","name":"ammaarpatel99"},"datePublished":"2021-02-07T12:06:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/2753/angularfire/issues/2753"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4cd68d69-4930-d0db-f0b6-34a4c87240a7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF52:238CA7:4B00101:61DC939:6A5D605E |
| html-safe-nonce | 493037ceaf60a5248ba81e86f9ba6cbf98d75247cc25968b76f21063d2dcfbbe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjUyOjIzOENBNzo0QjAwMTAxOjYxREM5Mzk6NkE1RDYwNUUiLCJ2aXNpdG9yX2lkIjoiMjY2NjMxNDU0ODA5MDA2OTA4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 997fcd5e6f25a74460388a30b29c1f4c84c9a8b99a4ad2374a89d3cd37d16974 |
| hovercard-subject-tag | issue:802937109 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angularfire/2753/issue_layout |
| twitter:image | https://opengraph.githubassets.com/84b32b9d5718eea8cb12fb9e5afacc1a0463031cfa512599e2114543668b78d3/angular/angularfire/issues/2753 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/84b32b9d5718eea8cb12fb9e5afacc1a0463031cfa512599e2114543668b78d3/angular/angularfire/issues/2753 |
| og:image:alt | Version info Angular: 11 Firebase: 8 AngularFire: 6 Other: MacOS Big Sur Issue When you call valueChanges on a document you know doesn't exist, it emits undefined. However if you specify idField, f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ammaarpatel99 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angularfire git https://github.com/angular/angularfire.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 49453413 |
| octolytics-dimension-repository_nwo | angular/angularfire |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49453413 |
| octolytics-dimension-repository_network_root_nwo | angular/angularfire |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width