简体   繁体   English

本地云功能Firebase将数据添加到Firestore

[英]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. 假设我想编写一个脚本,将数据添加到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 ? 有什么想法可以通过运行node在NodeJS上工作吗?

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. 虽然绝对可能在本地运行HTTPS触发的功能并与Firebase进行交互,但可能有更简单的方法可以实现目标。

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. 例如,如果您只想向Cloud Firestore添加一些初始数据,请考虑在本地节点脚本中使用Admin SDK来完成此操作。 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. 它使用了很多相同的部分,但是从混合中删除了Cloud Functions,从而降低了复杂性并减少了您需要学习的东西。

To see how to interact with Cloud Firestore from a Node.js script, check the node.js tabs in the Firestore documentation . 要查看如何通过Node.js脚本与Cloud Firestore进行交互,请查看Firestore文档中node.js选项卡。 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. 对于您的特定脚本,您可以删除第一行export const dummyData = https.onRequest(req, res => { ,它特定于Cloud Functions,仅保留与Firestore相关的代码。

暂无
暂无

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

相关问题 从云中的 Firebase Firestore 获取数据 Function - Get Data from Firebase Firestore in Cloud Function 使用 firebase 云 function 从云 Firestore 读取数据? - Read data from cloud firestore with firebase cloud function? 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function Firebase Cloud Function:Cloud Firestore 查询无效,即使数据在 Cloud Firestore 中 - Firebase Cloud Function : Cloud Firestore query invalid eventhough data is in Cloud Firestore Firebase:Firestore 未更新 Cloud Function 的计划函数中的数据 - Firebase : Firestore not updating data in Cloud Function's scheduled function 使用 Google Cloud Function 将数据从 firebase firestore 导出到 MeiliSearch - Exporting data from firebase firestore to MeiliSearch using Google Cloud Function 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore 在 Firebase Cloud Firestore 中构建数据 - Structuring Data in Firebase Cloud Firestore 用于导出 Firestore 备份数据的云功能。 使用 firebase-admin 还是 @google-cloud/firestore? - Cloud function to export Firestore backup data. Using firebase-admin or @google-cloud/firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM