Firebase 如何在使用批处理时从firestore获取数据?
我想在firestore中执行批处理事务。我正在其他收藏中存储最后一把钥匙。 我需要得到最后一个键,然后增加1,然后使用这个键创建两个文档。我该怎么做Firebase 如何在使用批处理时从firestore获取数据?,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,我想在firestore中执行批处理事务。我正在其他收藏中存储最后一把钥匙。 我需要得到最后一个键,然后增加1,然后使用这个键创建两个文档。我该怎么做 let lastDealKeyRef = this.db.collection('counters').doc('dealCounter') let dealsRef = this.db.collection('deals').doc(id) let lastDealKey = batch.get(lastDealKeyRef) // he
let lastDealKeyRef = this.db.collection('counters').doc('dealCounter')
let dealsRef = this.db.collection('deals').doc(id)
let lastDealKey = batch.get(lastDealKeyRef) // here is the problem..
batch.set(dealsRef, dealData)
let contentRef = this.db.collection('contents').doc('deal' + id)
batch.set(contentRef, {'html': '<p>Hello World</p>' + lastDealKey })
batch.commit().then(function () {
console.log('done') })
让lastDealKeyRef=this.db.collection('counters').doc('dealCounter'))
let dealsRef=this.db.collection('deals').doc(id)
让lastDealKey=batch.get(lastDealKeyRef)//问题出在这里。。
batch.set(dealsRef、dealData)
让contentRef=this.db.collection('contents').doc('deal'+id)
set(contentRef,{'html':'helloworld'+lastdaleKey})
batch.commit().then(函数(){
console.log('done')})
如果要在单个操作中读取/写入数据,则应使用事务
// Set up all references
let lastDealKeyRef = this.db.collection('counters').doc('dealCounter');
let dealsRef = this.db.collection('deals').doc(id);
let contentRef = this.db.collection('contents').doc('deal' + id);
// Begin a transaction
db.runTransaction(function(transaction) {
// Get the data you want to read
return transaction.get(lastDealKeyRef).then(function(lastDealDoc) {
let lastDealData = lastDealDoc.data();
// Set all data
let setDeals = transaction.set(dealsRef, dealData);
let setContent = transaction.set(contentRef, {'html': '<p>Hello World</p>' + lastDealKey });
// Return a promise
return Promise.all([setDeals, setContent]);
});
}).then(function() {
console.log("Transaction success.");
}).catch(function(err) {
console.error("Transaction failure: " + err);
});
//设置所有引用
让lastDealKeyRef=this.db.collection('counters').doc('dealCounter');
让dealsRef=this.db.collection('deals').doc(id);
让contentRef=this.db.collection('contents').doc('deal'+id);
//开始交易
db.runTransaction(函数(事务){
//获取要读取的数据
return transaction.get(lastDealKeyRef).then(函数(lastDealDoc){
设lastDealData=lastDealDoc.data();
//设置所有数据
设setDeals=transaction.set(dealsRef,dealData);
让setContent=transaction.set(contentRef,{'html':'helloworld'+lastdaleKey});
//还愿
返回承诺。全部([setDeals,setContent]);
});
}).然后(函数(){
log(“事务成功”);
}).catch(函数(err){
控制台错误(“事务失败:+err”);
});
您可以在此处阅读有关事务和批处理的更多信息:
但是有一点需要注意——目前不支持离线交易,我希望将来会有所改变:)