I want to implement <firebase-document> to fetch and apply user settings. I am using this page as a guide.
When I load the page with no data at the /users/{{userId}}/settings node in my firebase, I expect to see the object { new: 'initial', j: 5, o: 'N' }) loaded there. However, I actually see no change to the node in my firebase.
What am I doing wrong and how can I achieve my desired bahavior?
settings.html<firebase-document
path="/users/{{userId}}/settings"
data="{{data}}">
</firebase-document>
<script>
Polymer({
properties: {
uid: String,
data: {
type: Object,
observer: 'dataChanged'
}
},
dataChanged: function (newData, oldData) {
// if settings have not been set yet, initialize with initial value
if(!newData && !oldData){
this.set('data', { new: 'initial', j: 5, o: 'N' });
}
}
});
</script>
Edit: Answering the questions by @HakanC's comment:
Does
{{userId}}have a value when executed?
Yes.
Is there data at the path
/users/{{userId}}/settings?
No. There is no /settings node when the user first logs in. But I do have code that successfully creates a node at /users/{{userId}}. And that node is present when this element executes its script.
Console log whether you arrive
this.setline.
I do arrive there—multiple times. The first time, the logged value of data is undefined. The second time, the logged value of data is {}.
As you mentioned above that there is no data at the path, then you need to change if conditions. Something like:
instead of;
EDIT:
DEMO