I'm trying to push data to Firebase using Polymerfire and the Polymer App Toolbox template:
my-app.htmlcontains a configured<firebase-app>component.my-models.html(which is a page configured inmy-app.htmlroutes) contains a<add-model>component<add-model>is a form with a<firebase-query>,<firebase-auth>, input, and submit button.I attempt to push data using
<firebase-query>with the following function in theadd-model.htmlPolymerobject:Polymer({ is: 'add-model', properties: { data: { type: Object } }, create: function() { this.$.query.ref.push({ name: this.$.myModelName.value }); } });Outcome
Calling
pushon the<firebase-query>object inadd-model.htmlreturnsCannot read property 'push' of undefined(explained in this Q/A, in our case it's because<firebase-app>does not appear to be properly declared).- Moving
<firebase-app>frommy-app.htmltoadd-model.htmlfile does make thepushfunction work, but thenmy-app.htmlloses its Firebase functionality. - Calling
<firebase-app>in both my-app.html and add-model.html returnsUncaught Error: Firebase App named 'firebase-app' already exists. <firebase-auth>does work inadd-model.htmland returns a[[user]]object.
How do I make firebase-query properly communicate with firebase-app from my route?
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.htmlprobably importspolymerfire/firebase-app.html(and no other imports from Polymerfire); andmy-models.htmlprobably imports onlypolymerfire/firebase-query.htmlandpolymerfire/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.htmlis not imported in the same file as the declaration of<firebase-app>.polymerfire.htmlimports all elements from Polymerfire, includingPolymer.FirebaseCommonBehavior, which defines theappproperty seen in all Polymerfire elements. I'm guessing the Firebase SDK requires all Polymerfire elements imported beforeappinitialization in order to populate theappobject appropriately. Otherwise, whenfirebase-queryinitializes,app.databasewill be undefined, which prevents itsrefproperty from initializing, leading to runtime errors when you try to use the methods of the Polymerfire elements. In the case ofthis.$.query.ref.push(...), the error seen would be: