简体   繁体   English

Firestore 通过函数将文档 ID 添加到数据

[英]Firestore add Document ID to Data via functions

I'm looking to add the Firestore ID to the DocumentData, so that I can easily utilize the ID when referring to rows in a table, without having to use document.data().property everytime I call a property of a document.我希望将 Firestore ID 添加到 DocumentData,这样我就可以在引用表中的行时轻松使用该 ID,而不必每次调用文档的属性时都使用 document.data().property。 Instead, I want to be able to call document.id.... document.property... and so on.相反,我希望能够调用 document.id.... document.property... 等等。

Is there an easy way to do this?是否有捷径可寻? Possibly with a Cloud Function that adds the auto-generated ID to the document data?可能是云 Function 将自动生成的 ID 添加到文档数据中?

Thanks!谢谢!


Example:例子:

export const getSpaces = async () => {
  const spaceDocs = await getDocs(spacesCollection)
  spaceDocs.docs.forEach((spaceDoc) => {
    const spaceID = spaceDoc.id
    const spaceData = spaceDoc.data()
    console.log(spaceID)
    
    spaces.value.push(spaceData)
  })
}

Now, the spaces array has objects containing the data of the documents.现在,spaces 数组具有包含文档数据的对象。 But, I loose the ability to reference the ID of a document.但是,我失去了引用文档 ID 的能力。

Alternatively, I can add the entire document to the array, but following that, I'll have to access the properties by always including the data() in between.或者,我可以将整个文档添加到数组中,但之后,我必须始终通过在两者之间包含 data() 来访问属性。 Ie space.data().name即 space.data().name

I'm certain, theres a better way我敢肯定,有更好的方法

A DocumentSnapshot has id property that you are looking for. DocumentSnapshot具有您要查找的id属性。 If you add something within the document, then you'll need to access the data first with doc.data().field_name .如果您在文档中添加内容,则需要先使用doc.data().field_name访问数据。

You don't need Cloud Functions to add the document ID to the data of that document.您不需要 Cloud Functions 即可将文档 ID 添加到该文档的数据中。 If you look at the third code snippet in the documentation on adding a document , you can see how to get the ID before writing the document.如果您查看有关添加文档的文档中的第三个代码片段,您可以了解如何在编写文档之前获取 ID。

In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later.在某些情况下,创建具有自动生成的 ID 的文档引用并在以后使用该引用可能很有用。 For this use case, you can call doc() [without any arguments]:对于此用例,您可以调用doc() [不带任何参数]:

const newCityRef = db.collection('cities').doc(); // 👈 Generates a reference, but doesn't write yet

// Later...
const res = await newCityRef.set({
  newCityRef.id, // 👈 Writes the document ID
  // ...
});

As others have commented, you don't need to store the ID in the document.正如其他人评论的那样,您不需要将 ID 存储在文档中。 You can also add it to your data when you read the documents, with:您还可以在阅读文档时将其添加到您的数据中,方法是:

spaceDocs.docs.forEach((spaceDoc) => {
  const spaceID = spaceDoc.id
  const spaceData = spaceDoc.data()
  console.log(spaceID)
  
  spaces.value.push({ id: spaceID, ...spaceData })
})

With this change your spaces contains both the document ID and the data of each document.通过此更改,您的spaces将同时包含文档 ID 和每个文档的数据。

Working on another problem, I came across the solution.在处理另一个问题时,我遇到了解决方案。 Quite simple actually:其实很简单:

When declaring the new Object, you can add ...doc.data() to add all the properties of the DocumentData to the newly created Object, after initialising it with an id.在声明新的 Object 时,您可以添加...doc.data()以将 DocumentData 的所有属性添加到新创建的 Object 中,在使用 id 对其进行初始化后。 This works for me anyways.无论如何,这对我有用。 Case closed.结案。

onSnapshot(profilesCollectionRef, (querySnapshot) => {
  const profilesHolder = [];
  querySnapshot.forEach((doc) => {
    const profile = {
      id: doc.id,
      ...doc.data(),
    }
    profilesHolder.push(profile);
    console.log(profile);
  });
            
  profiles.value = profilesHolder;
});
    
onSnapshot(doc(db, "profiles", userId), (doc) => {
  const newProfile = {
    id: doc.id,
    ...doc.data(),
  }
  myProfile.value = newProfile as Profile;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 firestore 9 中将带有自定义 ID 的文档添加到 firestore - How to add Document with Custom ID to firestore in firestore 9 Flutter Firestore:将数据列表添加到 Firestore 文档 - Flutter Firestore : Add a List of Data to Firestore Document 如何在 filds firestore 中添加文档的 id - how to add id of document in filds firestore 没有 id 的 firestore 文档更新数据 - firestore document update data without id Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions 使用 angular 在 Firebase Cloud Functions (Firestore) 中获取单个文档 ID - Getting single document ID in Firebase Cloud Functions (Firestore) with angular Flutter firestore 在文档中添加集合和数据 - Flutter firestore add collection and data in document Firebase 函数:参数“数据”的值不是有效的 Firestore 文档 - Firebase functions: Value for argument "data" is not a valid Firestore document 如何在一次又一次地点击提交按钮后通过生成不同的文档 ID 将数据添加到 firebase 火库? Android Kotlin - How to add data to the firebase firestore by generating different document id after hitting submit button again and again? Android Kotlin 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM