问题
I'm following a Polymer + Firebase tutorial video using the Polymer App Tool Kit (initiated with the polymer CLI).
When I try to push data to a Firebase collection, I get the following error:
Cannot read property 'push' of undefined'
Here's my code (firebase app is initiated in my-app.html with name "firebase-app"):
<dom-module id="add-model">
<!-- Defines the element's style and local DOM -->
<template>
<firebase-auth user="{{user}}" app-name="firebase-app"></firebase-auth>
<firebase-query
app-name="firebase-app"
id="query"
path="/users/[[user.uid]]/models"
data="{{model}}">
</firebase-query>
<paper-input id="modelName" label="Model Name" "></paper-input>
<paper-button class="create" id="create" on-tap="create" raised>Create</paper-button>
</template>
<!-- Creates the element's prototype and registers it -->
<script>
Polymer({
is: 'add-model',
properties: {
data: {
type: Object
}
},
create: function() {
this.$.query.ref.push({
name: this.$.modelName.value
});
}
});
</script>
</dom-module>
回答1:
First, I recommend comparing your code to the Polycast #58 source in GitHub, which might reveal the problem more clearly to you.
this.$.query.ref
(<firebase-query>.ref
) refers to the ref property of Polymer.FirebaseDatabaseBehavior
, which is computed with:
__computeRef: function(db, path) {
if (db == null ||
path == null ||
!this.__pathReady(path) ||
this.disabled) {
return null;
}
return db.ref(path);
},
Note that db.ref(path)
never returns null, so ref
being null
must be a result of the if
statement. Evaluating each condition might uncover the issue:
db == null
- This property is computed from
app.database()
(never returns null), or is set tonull
when app is not defined (by<firebase-app>
). Have you declared<firebase-app>
with the properapp-name
before importing the element that uses<firebase-query>
?
- This property is computed from
path == null
- You might not have set <firebase-query>.path. This is clearly not the case.
!this.__pathReady(path)
- One of the path components might be an empty string or uninitialized. For your path (
/users/[[user.uid]]/models
), it's possible thatuser
is not yet defined (user is not yet logged in), which results in an empty string in that path component (/users//models
).
- One of the path components might be an empty string or uninitialized. For your path (
this.disabled
- You might have set <firebase-query>.disabled. This is clearly not the case.
来源:https://stackoverflow.com/questions/41435694/polymer-firebase-polymerfire-cannot-read-property-push-of-undefined-th