
[英]Interacting with firebase firestore from firebase cloud functions
[英]cloud functions: trying to create user on firestore from cloud functions "failed to deploy"
我正在尝试根据 http 请求访问我的数据库。
在来自谷歌的 api 构建器中,我使用 node.js 16 作为运行时。
我尝试运行此代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firestore);
const firestoreDB = admin.firestore()
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase Cloud Functions!");
console.log("function triggered")
});
exports.createUser = functions.firestore.document('Users/asddsa')
.onCreate((snap, context) => {
const newValue = snap.data();
if (snap.data() === null) return null;
const uid = context.params.userId
let notificationCollectionRef = firestoreDB.collection('Users').doc(uid).collection('Notifications')
return notificationCollectionRef.add({
notification: 'Hello Notification',
notificationType: 'Welcome'
}).then(ref => {
return console.log('notification successful', ref.id)
})
});
但我什至无法部署它,它只是说“部署失败”。
现在这通常是在代码中有错字的时候。 但我猜我没有建立与firestoreproperley的连接。 (我从来没有给它密码或任何东西)
我假设因为它在同一个项目中,所以连接会以任何一种方式工作,但也许我错了?
如何设置连接以创建用户而不导致部署失败?
为 Firebase 项目配置和设置 Cloud Functions 的初始设置说明。 您可以查看Firebase 文档。
您可以在Cloud firestore trigger中查看详细信息。 其中描述了事件触发器,您可以在其中触发 function 以在任何时候使用 onCreate() 在集合中创建新文档时触发。 每次添加新的用户配置文件时,此 function 都会调用 createUser。
您还可以查看Github 链接来创建用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.