在JS中使用Cloud Functions将数据设置为Firestore [英] Setting Data to Firestore with Cloud Functions in JS

查看:32
本文介绍了在JS中使用Cloud Functions将数据设置为Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新用户后,我想将数据添加到Firestore中的新集合中.

Upon the creation of a new user, I would like to add data to a new collection in Firestore.

我已经这样设置了功能;

I have my functions set up as such;

exports.createUser = functions.firestore.document('Users/{userId}')
    .onCreate((snap, context) => {
        const newValue = snap.data();
        if (snap.data() === null) return null;
        const userRef = context.params.userId
        console.log("create user found " + (userRef))
        let notificationCollectionRef = firestoreDB.collection('Users').document(userRef).collection('Notifications')
        let notificationDocumentRef = notificationCollectionRef.document('first notification')
        return notificationDocumentRef.set({
            notifications: "here is the notificiation"
        }, {merge: true});
});

运行该功能时,我得到了控制台日志,正确打印了userId,但是出现了以下错误;

When running the function I get the console log correctly printing out the userId but I get the following error;

TypeError:firestoreDB.collection(...).document不是函数在exports.createUser.functions.firestore.document.onCreate(/user_code/index.js:23:73)在对象.(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在下(本机)在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71在__awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)在cloudFunction(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在/var/tmp/worker/worker.js:728:24在process._tickDomainCallback(internal/process/next_tick.js:135:7)

TypeError: firestoreDB.collection(...).document is not a function at exports.createUser.functions.firestore.document.onCreate (/user_code/index.js:23:73) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:728:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我是JS&的新手.职能.一如既往,任何帮助都将不胜感激.

I'm new to JS & Functions. As always any help greatly appreciated.

推荐答案

以下是完整的工作代码:

Here is the full working code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firestore);

const firestoreDB = admin.firestore()

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase Cloud Functions!");
 console.log("function triggered")
});

exports.createUser = functions.firestore.document('Users/{userId}')
    .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)
            })
        });

这篇关于在JS中使用Cloud Functions将数据设置为Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆