简体   繁体   English

将所有项目作为单独的文档保存到 firebase 集合

[英]Saving all items to a firebase collection as individual documents

I have a function where a user types in there sitemap and it returns all the links in there sitemap.我有一个 function,用户在其中键入站点地图,它会返回站点地图中的所有链接。

How can I then save all of those links in individual documents in a specific collection on Firebase?然后如何将所有这些链接保存在 Firebase 上特定集合中的单个文档中?

See below for the current firebase set function. Erro returns "Function DocumentReference.set() called with invalid data. Data must be an object, "请参阅下面的当前 firebase 集 function。错误返回“使用无效数据调用的函数 DocumentReference.set()。数据必须是 object,”

const saveAllCrawl = async () => {
     await db.collection('urls').doc('test').set(urls);
}

Current map of the array the sitemap crawler returns & Scrap Sitemap function站点地图爬虫返回数组的当前 map & 废弃站点地图 function

const scrapeSitemap =  async (e) => {
  e.preventDefault();
  console.log(sitemapURL)
  const array = await GetSitemapLinks(sitemapURL);
  setURLS(array)
}

{urls && urls.map((user) => (
      <Grid container spacing={0} >
                    <Grid  xs={6} sm={6} md={6}>
                        <li className="user">{user}</li>
                    </Grid>
      </Grid>
))}
    <Button onClick={saveAllCrawl}>Save All To Firebase</Button>

In document every value is being stored against a key.在文档中,每个值都存储在一个键上。

Like if there are multiple urls and you want to store each in a separate document so you have to stored them against any key.就像如果有多个 url 并且你想将每个 url 存储在一个单独的文档中,那么你必须将它们存储在任何键上。 Collections: URLS Document Id: (generated_key) Document Data: Collections:URLS 文档 ID:(generated_key)文档数据:

{ "key" : "url_value" }

So, as mentioned above for this you have to create a batch in which you assign all queries to a batch and after that set batch.commit().因此,如上所述,您必须创建一个批处理,在其中将所有查询分配给一个批处理,然后设置 batch.commit()。

Now, if you are trying to store them in a list like an array.现在,如果您尝试将它们存储在像数组一样的列表中。

{
   "urls": [
   "url_one",
   "url_one"
   ]
}

So, first create an object and assigned all string values to key and then set that to a document.因此,首先创建一个 object 并将所有字符串值分配给键,然后将其设置为文档。

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

相关问题 Firebase 函数:更新集合中的所有文档 - Firebase Functions: update all documents inside a collection 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 如何检索 Firebase 中集合中的所有文档并添加到列表中? - How to retrieve all documents in a collection in Firebase and add to a list? Firebase Firestore:有没有办法为集合中的所有文档强制执行必填字段? - Firebase Firestore: Is there a way to enforce required fields for all documents in a collection? 如何获取 Firebase 集合中具有特定值的所有文档? - How to get all documents in Firebase collection with specific value? 我想用 firebase 函数重写集合中的所有文档 - I want to rewrite all documents in a collection with firebase functions 如何获取 Firebase 集合中的所有文档(包括不存在的祖先文档)? - How do you fetch all documents (including non-existent ancestor documents) in a Firebase collection? Firebase 不从某些集合的集合中获取文档 - Firebase not fetching documents from collection for some collection 删除 Firestore 集合中的所有文档 - Deleting all documents in Firestore collection Firebase 数据库集合在尝试获取所有文档时返回空数组 - Firebase database collection returns empty array when trying to get all documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM