[英]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.