Retrieve data from Firebase to Polymer element

ぐ巨炮叔叔 提交于 2019-12-13 07:01:05

问题


I'm having trouble getting data from firebase to an polymer element.

Here is my code:

 <dom-module id="bb-account-settings">

  <template>

    <style is="custom-style" include="shared-styles"></style>

                <div class="settings-dialog">
                    <div class="frame">
                        <div class="horizontal layout">
                            <div>
                                <div class="user-image"></div>
                            </div>
                            <div class="horizontal layout">
                                <paper-input label="First Name" value="{{fNameSet}}"></paper-input>
                                &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                                <paper-input label="Last Name" value="{{lNameSet}}"></paper-input>
                            </div>
                            <paper-input label="Email" value="{{emailSet}}"></paper-input>
                        </div>
                        <paper-input label="Phone number" value="{{phoneSet}}" allowed-pattern="[0-9]"></paper-input>
                    </div>
                </div>

  </template>
  <script>

    Polymer({

      is: 'bb-account-settings',

            properties: {
                fNameSet: {
                    type: String,
                    value: function() {
                        var userId = firebase.auth().currentUser.uid;
                        firebase.database().ref('/user/' + userId + '/UserInfo').once('value').then(function(snapshot) {
                            console.log(snapshot.val().fName)
                            return (snapshot.val().fName);
                        });
                    }
                },
                lNameSet: {
                    type: String,
                    value: 'test last name'
                    }
                }
            }

    });

  </script>

lNameSEst is 'test last name' and obviously its put in its input filed. You well expect fNameSet to to the same thing, but no. when I print it (console.log) it says 'test first name' but it doesn't show in its input filed!

I'm so confused.


回答1:


As one of the commenters noticed, returning a value in a callback is the problem. Your function passed to firebase's "once" function is called asynchronously & in a different scope so basically it loses it's relationship with where you declared it & the value you're returning isn't going to where you're expecting.

A somewhat brute force way to get what you want is to do something like the following:

ready: function() {
    var userId = firebase.auth().currentUser.uid;
    firebase.database().ref('/user/' + userId + '/UserInfo').once('value').then(function(snapshot) {
          console.log(snapshot.val().fName)
          this.fNameSet = snapshot.val().fName;
        }.bind(this));
}

There are more elegant approaches but it is sort of out of scope of this question & requires some re-structuring & deeper insight into your app's structure.



来源:https://stackoverflow.com/questions/38368392/retrieve-data-from-firebase-to-polymer-element

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