问题
I'm using the firebase/polymerfire package from bower bower install firebase/polymerfire
how can i create some data in the database after a method has been triggered?
The document tag looks like it will display and update data. I would like to, when a user signs up, create some data for the user to use.
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
};
How can i use the default set() or push methods like the normal SDK?
How can i call this on an event from JavaScript?
When trying to bind a path to my document like
<firebase-document
path="/"
data="{{firebaseData}}">
</firebase-document>
{{firebaseData}}
the data won't display, but I have authentication working.
回答1:
You can actually use the firebase api directly there since firebase-auth
is already including it, but if you want to keep the element-based functionality you could do this:
Add an empty firebase-document
to your template
<firebase-document id="mydoc"></firebase-document>
Then call its save
function in your element
app.signInAnonymously = function() {
this.error = null;
this.$.auth.signInAnonymously();
// add default data for the user template
//set path to null somewhere to avoid overwriting data, I recommend doing it here since save's path update is lazy
this.$.mydoc.path = null;
//set data to the value to set/push
this.$.mydoc.data = {/*your data*/};
//call save, if the second parameter is falsey it'll call push to the first parameter if it's truthy it'll set the data to firstparam/secondparam
this.$.mydoc.save('path/to/users', userid);
};
As for getting data with firebase-document
, check that you actually have data in your database and your security rules, if you still can't see your data then it might be related to this issue, do bear in mind that polymerfire
is still in a pre-release state
来源:https://stackoverflow.com/questions/37711115/polymerfire-creating-data-after-an-event