Polymer + Firebase (Polymerfire): Cannot read property 'push' of undefined': 'this.$.query.ref.push(…)'

[亡魂溺海] 提交于 2020-01-28 10:14:11

问题


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 to null when app is not defined (by <firebase-app>). Have you declared <firebase-app> with the proper app-name before importing the element that uses <firebase-query>?
  • 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 that user is not yet defined (user is not yet logged in), which results in an empty string in that path component (/users//models).
  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!