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