简体   繁体   中英

Local Cloud Function Firebase add data to Firestore

I just wanted to ask if its possible to make a cloud function locally without having to deploy the function. Let's say I wanted to make a script on adding data to firestore. Initially here is my code if I wanted to make a request. But since this is just for setting up dummy data I wanted to make the script run locally. Any idea on working this one on NodeJS by running node ?

export const dummyData = https.onRequest(req, res => {

const data = [{id: 1, name: "Name 1"]},{id: 2,name: "Name 2"},{...}]

  for (const d of data) {
    const dRef = admin.firestore.collection("data").doc(d.id);
    batch.set(dRef, { name: d.name });
  }

return batch.commit().then(result => {
    res.send(200)
}    
}).catch(err => {
    res.send(500)
});

While it's definitely possible to run a HTTPS-triggered function locally and interact with Firebase, there are probably simpler ways to reach your goal.

For example, if all you want is to add some initial data to Cloud Firestore, consider using the Admin SDK in a local node script to accomplish the same. That uses a lot of the same parts, but removes Cloud Functions from the mix, which reduces complexity and the number of things you'll need to learn.

To see how to interact with Cloud Firestore from a Node.js script, check the node.js tabs in the Firestore documentation . For your specific script you can remove the first line export const dummyData = https.onRequest(req, res => { , which is specific to Cloud Functions and be left with only the code that relates to Firestore.

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