我想将新集合添加到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()
答案 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 );