将项目ID添加到集合中时,我可以创建它吗?

时间:2019-07-31 14:41:02

标签: javascript firebase google-cloud-firestore

在Firebase控制台上将文档添加到集合中时,我可以输入自己的ID或选择要自动生成的ID。到目前为止,一切都很好。 我想知道的是,在代码中添加文档时是否可以选择该文档的ID。 当前代码如下:

 return db
    .collection('items')
    .add({
      attribute1: 'something',
      attribute2: 'something Else'
    })

我想要的是实际选择通过代码添加的项目的ID,而不是仅仅给我自动生成的ID。这可能吗?如果是这样,怎么做?

1 个答案:

答案 0 :(得分:2)

如果您按照问题标签的建议使用Firebase实时数据库,则可以按照here

中的说明进行操作
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });

但是,如果您按照问题代码段的建议使用Firestore,则可以按照here

进行操作
db.collection("users").doc(userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
相关问题