代码之家  ›  专栏  ›  技术社区  ›  David Henry

在JS中使用云函数将数据设置为FireStore

  •  0
  • David Henry  · 技术社区  · 8 年前

    创建新用户后,我想将数据添加到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,但是我得到了以下错误;

    类型错误:firestoredb.collection(…).document不是函数。 在exports.createuser.functions.firestore.document.oncreate上(/user_code/index.js:23:73) 在对象处。(/user_code/node_modules/firebase函数/lib/cloud函数.js:112:27) 下一个(本机) at/user_code/node_modules/firebase functions/lib/cloud functions.js:28:71 at_uuu等待程序(/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(内部/process/next_Tick.js:135:7)

    我不熟悉JS&函数。一如既往,任何帮助都非常感谢。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Doug Stevenson    8 年前

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

        let notificationCollectionRef = firestoreDB.collection('Users').doc(userRef).collection('Notifications')
        let notificationDocumentRef = notificationCollectionRef.doc('first notification')
    
        2
  •  2
  •   David Henry    8 年前

    以下是完整的工作代码:

    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)
                })
            });
    推荐文章