Polymer + Firebase (Polymerfire): <firebase-query> not working inside single page app view (with <firebase-app> located in my-app.html)

我们两清 提交于 2019-11-29 05:14:15

TLDR: The solution is to import polymerfire/polymerfire.html (even if not all elements used in that file) in the same file as your <firebase-app> declaration.


Your my-app.html probably imports polymerfire/firebase-app.html (and no other imports from Polymerfire); and my-models.html probably imports only polymerfire/firebase-query.html and polymerfire/firebase-auth.html, assuming those are the only Polymerfire elements used in that file.

I've identified the cause (but not necessarily the root cause). The symptoms are exhibited when polymerfire.html is not imported in the same file as the declaration of <firebase-app>.

polymerfire.html imports all elements from Polymerfire, including Polymer.FirebaseCommonBehavior, which defines the app property seen in all Polymerfire elements. I'm guessing the Firebase SDK requires all Polymerfire elements imported before app initialization in order to populate the app object appropriately. Otherwise, when firebase-query initializes, app.database will be undefined, which prevents its ref property from initializing, leading to runtime errors when you try to use the methods of the Polymerfire elements. In the case of this.$.query.ref.push(...), the error seen would be:

Uncaught TypeError: Cannot read property 'push' of undefined

I found the above answer to import polymerfire/polymerfire.html right on the money. Additionally I would add that my particular app was very very fussy about exactly where every import and firebase element was located. Most of the polycasts (such as #58 referenced above) seem quite straightforward and easy to get right. This particular one took a lot of experimentation with where the elements and imports were located, due to scoping or visibility or other issues that I never figured out.

Hope this helps someone else in the same situation I was in.

I'd also bet that a visitor in 2018 or later would find this post obsolete, because it would seem likely to be improved by then.

For me the key was to declare firebase-app before declaring the rest of the WebComponents that had firebase-query or firebase-document inside. During debugging I realized that the firebase properties of the child WebComponents were set before they were declared in the firebase-app element itself.

This is the order that worked for me:

<firebase-app
    name='someName'
    auth-domain="xxx.firebaseapp.com"
    database-url="xxx.firebaseio.com/"
    api-key="my-api-key">
</firebase-app>

And all the elements that use query or document should come only after this:

<some-component>
...
   <firebase-document
       app-name='someName'
       id="query"
       path="/app/firstName"
       data="{{testFirstName}}">
   </firebase-document> 
...
</some-component>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!