云功能,用于将新集合添加到firestore中的现有文档

时间:2017-11-29 06:53:26

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

我想将新集合添加到firestore中已存在的文档中。是否可能?以下是执行此操作的代码,我使用云功能来执行此操作。无论何时在firestore中创建文档,都必须触发以下云功能

const functions = require('firebase-functions');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.myWallet = functions.firestore
    .document('Samyata/{authid}')
        .onCreate(event =>{
        const ID = event.params.authid
        const db = admin.firestore();
        var data = {
        Bitcoins : '0',
        Ether : '0',
        deyaCoins : '0'
        };
        var docRef = db.collection('Samyata').doc(ID);
        var updateDoc = docRef.update({
        db.collection('Samyata').doc(ID).collection('Wallet').doc(ID).set(data);});
        //var updateRef = docRef.update();
        });//end of onCreate() 

3 个答案:

答案 0 :(得分:3)

试试这个:)

return db.collection('Samyata').doc(ID).collection('Wallet').doc(ID).set(data);

答案 1 :(得分:1)

或者您可以像这样连接路径

return db.collection(`Samyata/${ID}/Wallet/${ID}`).add(data)

答案 2 :(得分:0)

transactionData= {
   TimeStamp: "123",
   SomeBooleanValue: false
}


var addDocToNewCollResult= admin.firestore().collection('CollectionLevel1').doc(userID).collection('CollectionLevel2').doc(transactionID).collection('CollectionLevel3').doc("OtherID").set( transactionData );