目录

Firebase - 写入列表数据( Write List Data)

在上一章中,我们向您展示了如何在Firebase中编写数据。 有时您需要为数据设置唯一标识符。 如果要为数据创建唯一标识符,则需要使用push方法而不是set方法。

推送方法

push()方法将在push()数据时创建唯一的id。 如果我们想要使用唯一ID创建前面章节中的玩家,我们可以使用下面给出的代码段。

var ref = new Firebase('https://tutorialsfirebase.firebaseio.com');
var playersRef = ref.child("players");
playersRef.push ({
   name: "John",
   number: 1,
   age: 30
});
playersRef.push ({
   name: "Amanda",
   number: 2,
   age: 20
});

现在我们的数据会有所不同。 该名称将只是一个名称/值对,就像其他属性一样。

Firebase写入列表数据推送

关键方法

我们可以使用key()方法从Firebase获取任何密钥。 例如,如果我们想获取我们的集合名称,我们可以使用以下代码段。

var ref = new Firebase('https://tutorialsfirebase.firebaseio.com');
var playersRef = ref.child("players");
var playersKey = playersRef.key();
console.log(playersKey);

控制台将记录我们的集合名称(玩家)。

Firebase写入列表数据密钥

更多相关内容将在下一章中介绍。

↑回到顶部↑
WIKI教程 @2018