Polymer 1.x + Firebase 2.x:如何使用Polymerfire将数据推送或放入Firebase? [英] Polymer 1.x + Firebase 2.x: How to push or put data to Firebase using Polymerfire?

查看:159
本文介绍了Polymer 1.x + Firebase 2.x:如何使用Polymerfire将数据推送或放入Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Polymerfire,我想添加新的节点到Firebase而不会覆盖数据。 (我把这个叫做 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

这里是Polymerfire文档一>。但它并没有提到如何完成这种类型的 push put -type方法来将数据插入到













$ b

@motss的回答建议

 此$ DOC( '/电子邮件')。; 

而不是

  this。$。doc('/','emails'); 

但是这不起作用,因为它添加了一个随机自动键,如下所示:



州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 A

my-app
 |
 - emails
    |
    + email1@example,com
    + email2@example,com

And finish with this.

State B

my-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 B

my-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 B

my-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆