Polymer 1.x + Firebase 2.x:如何使用Polymerfire将数据推送或放入Firebase? [英] Polymer 1.x + Firebase 2.x: How to push or put data to Firebase using Polymerfire?
问题描述
push
或者 put
行为。) $ b $ pre $ my $ app
|
- 电子邮件
|
+ email1 @ example,com
+ email2 @ example,com
完成这个。
状态B
my-app
|
- 电子邮件
|
+ email1 @ example,com
+ email2 @ example,com
+ email3 @ example,com
+ email4 @ example,com
但是当我从状态A开始并执行此操作时:
< firebase-document
id =doc
app-name =app
data ={{data}}>
< / firebase-document>
...
this。$。doc.save('/','emails');
我拿出这个:
State B
my-app
|
- 电子邮件
|
+ email3 @ example,com
+ email4 @ example,com
开始的数据: email1 @ example,com
和 email2 @ example,com
已被删除。
$ b
my-app
|
- 电子邮件
|
- -hvOxpxwjpWHBYGj-Pzqw
|
+ email3 @ example,com
+ email4 @ example,com
添加的关键字: -hvOxpxwjpWHBYGj-Pzqw
从而破坏了数据结构的索引功能。
我在这里假设电子邮件被用作对象的键,而不是一个数组(因为在Firebase实时数据库中不能有数组)。如果是这种情况,您可能需要这样的东西:
< firebase-query
id =emails
app-name =my-app
path =/ emails
data ={{emails}}>
< / firebase-query>
< script>
// ...
this。$。emails.ref.child('email3 @ example,com')。set({new:'data'});
// ...
< / script>
如果您不想实际查询电子邮件,但只想插入数据,那很简单使用JS SDK:
$ $ $ $ $ c $ firebase.database(refs'emails')。child('email3 @ example, com()')。set({new:'data'});
对于第三个选项,可以使用< firebase-document> / code>带路径:
< firebase-document
app-name =my- app
path =/ emails / [[emailKey]]
data ={{emailData}}>
< / firebase-document>
< script>
// ...
this.emailKey ='email3 @ example,com';
this.emailData = {new:'data'};
// ...
< / script>
Using Polymerfire, I want to add new nodes to a Firebase without overwriting data. (I call this push
or put
behavior.)
In other words. I want to start with this:
State Amy-app
|
- emails
|
+ email1@example,com
+ email2@example,com
And finish with this.
State Bmy-app
|
- emails
|
+ email1@example,com
+ email2@example,com
+ email3@example,com
+ email4@example,com
But when I start with State A and do this:
<firebase-document
id="doc"
app-name="app"
data="{{data}}">
</firebase-document>
...
this.$.doc.save('/', 'emails');
I wind up with this:
State Bmy-app
|
- emails
|
+ email3@example,com
+ email4@example,com
Notice the starting data: email1@example,com
and email2@example,com
were deleted.
Here is the Polymerfire documentation. But it doesn't mention anywhere how to accomplish this type of push
or put
-type method to insert data into a node.
How can I accomplish this?
Edit
The answer by @motss suggests:
this.$.doc('/emails');
instead of
this.$.doc('/', 'emails');
But that does not work because it adds a random auto key as follows:
State Bmy-app
|
- emails
|
- -hvOxpxwjpWHBYGj-Pzqw
|
+ email3@example,com
+ email4@example,com
Note the added key: -hvOxpxwjpWHBYGj-Pzqw
thereby destroys the indexing feature of the data structure.
I'm going to assume here that the emails are used as keys for objects and are not an array (since you can't have arrays in a Firebase Realtime Database). If that's the case, you probably want something like:
<firebase-query
id="emails"
app-name="my-app"
path="/emails"
data="{{emails}}">
</firebase-query>
<script>
// ...
this.$.emails.ref.child('email3@example,com').set({new: 'data'});
// ...
</script>
If you don't want to actually query the emails but just want to insert data, that's easy as well using the JS SDK:
firebase.database().ref('emails').child('email3@example,com').set({new: 'data'});
For a third option, you can use <firebase-document>
with a path:
<firebase-document
app-name="my-app"
path="/emails/[[emailKey]]"
data="{{emailData}}">
</firebase-document>
<script>
// ...
this.emailKey = 'email3@example,com';
this.emailData = {new: 'data'};
// ...
</script>
这篇关于Polymer 1.x + Firebase 2.x:如何使用Polymerfire将数据推送或放入Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!