Firebase推送项自定义键

Mat*_*liz 2 firebase firebase-realtime-database angularfire2

我需要将一个对象推入节点关注者,但我没有找到方法.

我试试这个:

this.af.database.list(`profile/${user}/followers/`).push({[from]: true});
Run Code Online (Sandbox Code Playgroud)

但我的输出就是这个 在此输入图像描述

我需要下一个输出

在此输入图像描述

任何的想法?

Gri*_*orr 7

使用push()将使用自动生成的随机唯一ID将新节点推送到列表中.如果您需要设置特定值,则需要使用set():

this.af.database.object(`profile/${user}/followers/${from}`).set(true);
Run Code Online (Sandbox Code Playgroud)

在此示例中,我使用object()list()是直接创建引用,而不是直接创建要创建的列表中的值,以及${from}[from]代码中的值相同的值.

或者,您可以使用该update()方法:

this.af.database.object(`profile/${user}/followers`).update({[from]: true});
Run Code Online (Sandbox Code Playgroud)