firestore中的angularfire2事务和批写入
如何在angular2应用程序中使用firestore进行批量写入/运行事务firestore中的angularfire2事务和批写入,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,如何在angular2应用程序中使用firestore进行批量写入/运行事务 如果可能,我如何在angular2应用程序中将JS代码转换为TS代码。如果您使用的是AngularFirestore,首先您需要确保您的构造函数设置如下: constructor(private db: AngularFirestore) 现在,您可以使用db.firestore获取firebase.firestore实例 您只需从链接复制代码,并用db.firestore替换db。或者,您可以使用箭头功能使其更美
如果可能,我如何在angular2应用程序中将JS代码转换为TS代码。如果您使用的是AngularFirestore,首先您需要确保您的构造函数设置如下:
constructor(private db: AngularFirestore)
现在,您可以使用db.firestore获取firebase.firestore实例
您只需从链接复制代码,并用db.firestore替换db。或者,您可以使用箭头功能使其更美观:
使用事务更新数据:
// Create a reference to the SF doc.
const sfDocRef = db.firestore.collection("cities").doc("SF");
// Uncomment to initialize the doc.
// sfDocRef.set({ population: 0 });
db.firestore.runTransaction(transaction =>
// This code may get re-run multiple times if there are conflicts.
transaction.get(sfDocRef)
.then(sfDoc => {
const newPopulation = sfDoc.data().population + 1;
transaction.update(sfDocRef, { population: sfDoc.data().population + 1 });
})).then(() => console.log("Transaction successfully committed!"))
.catch(error => console.log("Transaction failed: ", error));
// Create a reference to the SF doc.
const sfDocRef = db.firestore.collection("cities").doc("SF");
db.firestore.runTransaction(transaction =>
transaction.get(sfDocRef).then(sfDoc => {
const newPopulation = sfDoc.data().population + 1;
if (newPopulation <= 1000000) {
transaction.update(sfDocRef, { population: newPopulation });
return newPopulation;
} else {
return Promise.reject("Sorry! Population is too big.");
}
}))
.then(newPop => console.log("Population increased to ", newPop)
).catch(err => console.error(err)); // This will be an "population is too big" error.
从交易中传递信息:
// Create a reference to the SF doc.
const sfDocRef = db.firestore.collection("cities").doc("SF");
// Uncomment to initialize the doc.
// sfDocRef.set({ population: 0 });
db.firestore.runTransaction(transaction =>
// This code may get re-run multiple times if there are conflicts.
transaction.get(sfDocRef)
.then(sfDoc => {
const newPopulation = sfDoc.data().population + 1;
transaction.update(sfDocRef, { population: sfDoc.data().population + 1 });
})).then(() => console.log("Transaction successfully committed!"))
.catch(error => console.log("Transaction failed: ", error));
// Create a reference to the SF doc.
const sfDocRef = db.firestore.collection("cities").doc("SF");
db.firestore.runTransaction(transaction =>
transaction.get(sfDocRef).then(sfDoc => {
const newPopulation = sfDoc.data().population + 1;
if (newPopulation <= 1000000) {
transaction.update(sfDocRef, { population: newPopulation });
return newPopulation;
} else {
return Promise.reject("Sorry! Population is too big.");
}
}))
.then(newPop => console.log("Population increased to ", newPop)
).catch(err => console.error(err)); // This will be an "population is too big" error.
很简单:
constructor(private db: AngularFirestore){}
inserting(){
//--create batch--
var batch = this.db.firestore.batch();
//--create a reference--
const userRef = this.db.collection('users').doc('women').ref;
batch.set(userRef , {
name: "Maria"
});
//--create a reference--
const userRef = this.db.collection('users').doc('men').ref;
batch.set(userRef , {
name: "Diego"
});
//--Others more 54 batch's--
//--finally--
return batch.commit();
}
这对我来说非常有效。这也有助于将.ref也放在末尾,或者batch.set抱怨参数是AngularFirestoreDocument而不是DocumentReference。谢谢