简体   繁体   English

从Firebase到Polymer元素检索数据

[英]Retrieve data from Firebase to Polymer element

I'm having trouble getting data from firebase to an polymer element. 我无法将数据从Firebase传输到聚合物元素。

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. lNameSEst是“测试姓氏”,显然是将其放入其输入字段中。 You well expect fNameSet to to the same thing, but no. 您很希望fNameSet可以用于相同的东西,但是不可以。 when I print it (console.log) it says 'test first name' but it doesn't show in its input filed! 当我打印它(console.log)时,它显示“ test first name”(测试名),但它不会显示在其输入字段中!

I'm so confused. 我很混乱。

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. 传递给firebase的“一次”函数的函数被异步调用并在其他范围内使用,因此从根本上讲,它失去了与声明它的位置的关系,并且所返回的值不会到达期望的位置。

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. 有更多优雅的方法,但这超出了此问题的范围,并且需要一些重新构造和对应用程序结构的更深入了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM