【问题标题】:Setting Data to Firestore with Cloud Functions in JS在 JS 中使用 Cloud Functions 将数据设置到 Firestore
【发布时间】:2018-12-23 02:58:19
【问题描述】:

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

我的功能是这样设置的;

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});
});

运行该函数时,我得到控制台日志正确打印​​出用户 ID,但出现以下错误;

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)

我是 JS 和函数的新手。一如既往,非常感谢任何帮助。

【问题讨论】:

  • let notificationCollectionRef = snap.ref.collection('Notifications')
  • 或者...let firestoreDB = snap.ref.firestore
  • @JamesPoag 得到同样的错误。

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

这是完整的工作代码:

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)
            })
        });

【讨论】:

  • 谢谢伙计,我已经测试了这段代码,它工作正常。但是您可以删除“const newValue = snap.data();”在与 null 比较之前...
【解决方案2】:

firestoreDB.collection('Users') 返回一个CollectionReference 对象。您正在尝试调用一个名为 document() 的方法,但正如您从 API 文档中看到的那样,没有这样的方法。我想您打算使用doc() 来构建DocumentReference

    let notificationCollectionRef = firestoreDB.collection('Users').doc(userRef).collection('Notifications')
    let notificationDocumentRef = notificationCollectionRef.doc('first notification')

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2020-10-03
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多