Polymer 1.x:如何在自定义行为中强制使用 firebase-document 获取数据
Polymer 1.x: How to fetch data using firebase-document imperatively inside a custom behavior
我正在尝试使用自定义行为中动态的、强制创建的 firebase-document
元素来获取数据。
我有以下代码。请注意第一个 console.log
按预期正确记录。但是第二个从不记录任何内容。我哪里错了?
我的-behavior.html
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymerfire/polymerfire.html">
...
getUserLocalSeries: function(uid) {
var doc = document.createElement('firebase-document');
var path = ['users', uid, 'local'].join('/');
doc.appName = 'app';
doc.path = path;
var _this = this;
console.log('path', path); // Logs fine
doc.addEventListener('response', function(e) {
console.log('doc', doc.lastResponse); // Doesn't log
_this.set('userLocalSeries', doc.lastResponse);
});
this.set('userLocalSeries', doc.data);
},
我强烈建议为此使用 Firebase JS SDK:
getUserLocalSeries: function(uid) {
var path = ['users', uid, 'local'].join('/');
var _this = this;
firebase.database().ref(path).on('value', function(snap) {
_this.set('userLocalSeries', snap.val());
});
}
只要您的项目中有firebase-app
,您就可以使用SDK。此示例要求您从 firebase-app
中省略 name
属性,但您也可以这样做firebase.app('app').database().ref()
相反。
我正在尝试使用自定义行为中动态的、强制创建的 firebase-document
元素来获取数据。
我有以下代码。请注意第一个 console.log
按预期正确记录。但是第二个从不记录任何内容。我哪里错了?
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymerfire/polymerfire.html">
...
getUserLocalSeries: function(uid) {
var doc = document.createElement('firebase-document');
var path = ['users', uid, 'local'].join('/');
doc.appName = 'app';
doc.path = path;
var _this = this;
console.log('path', path); // Logs fine
doc.addEventListener('response', function(e) {
console.log('doc', doc.lastResponse); // Doesn't log
_this.set('userLocalSeries', doc.lastResponse);
});
this.set('userLocalSeries', doc.data);
},
我强烈建议为此使用 Firebase JS SDK:
getUserLocalSeries: function(uid) {
var path = ['users', uid, 'local'].join('/');
var _this = this;
firebase.database().ref(path).on('value', function(snap) {
_this.set('userLocalSeries', snap.val());
});
}
只要您的项目中有firebase-app
,您就可以使用SDK。此示例要求您从 firebase-app
中省略 name
属性,但您也可以这样做firebase.app('app').database().ref()
相反。