简体   繁体   中英

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.

How can I then save all of those links in individual documents in a specific collection on Firebase?

See below for the current firebase set function. Erro returns "Function DocumentReference.set() called with invalid data. Data must be an object, "

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

Current map of the array the sitemap crawler returns & Scrap Sitemap 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. Collections: URLS Document Id: (generated_key) Document Data:

{ "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().

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM