如何解决从Firestore复制两个以上的收藏到另一个收藏不起作用
所以我的Firestore数据库中有四个集合:
- 英语问题
- MathQuestions
- 科学问题
- Dzongkha问题
我正在尝试复制这些集合中的所有数据,并为每个用户将其放在一个集合中。所以看起来像这样。
- 用户名(集合)
- MathQuestions(doc)-问题(集合)
- EnglishQuestions(doc)-问题(集合)
- ScienceQuestions(doc)-问题(集合)
- Dzongkha问题(doc)-问题(集合) 但是,当我尝试执行此操作时,它的行为很奇怪,有时是将所有四个集合都复制到用户名集合中,而有时仅复制前两个集合或第一个集合。
在我有一部分之前,您需要依次为每个集合调用数据库。由于这没有用,因此我尝试使每个调用都具有自己的功能,如下所示。可悲的是,这也不起作用。任何帮助将不胜感激。
接受@ralemos的建议后,它仍然不起作用,但是新代码如下所示。
fire
.auth()
.createUserWithEmailAndPassword(this.state.email,this.state.password)
.then((u) => {
let user = fire.auth().currentUser;
console.log(user);
if (user != null) {
user
.updateProfile({
displayName: this.state.name,})
.then((r) => {
let db = fire.firestore();
let data = {
name: this.state.name,email: this.state.email,college: this.state.college,dzongkhag: this.state.dzongkhag,};
db.collection(this.state.email).doc("UserProfile").set(data);
this.copyMathDatabase();
});
}
});
}
copyMathDatabase() {
let db = fire.firestore();
db.collection("Questions")
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
db.collection(this.state.email).doc("MathQuestions").collection("Questions").doc(doc.id).set({
Category: doc.data().Category,Choice: doc.data().Choice,CorrectAnswer: doc.data().CorrectAnswer,IsCorrectAnswer: doc.data().IsCorrectAnswer,IsWrongAnswer: doc.data().IsWrongAnswer,Question: doc.data().Question,UserHasNotResponded: doc.data().UserHasNotResponded,Marked: doc.data().Marked,});
console.log("Math Questions: ",doc.id);
});
this.copyEnglishDatabase();
console.log(" Done copying the Math database ");
});
}
copyEnglishDatabase() {
let db = fire.firestore();
db.collection("EnglishQuestions")
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
db.collection(this.state.email).doc("EnglishQuestions").collection("Questions").doc(doc.id).set({
Category: doc.data().Category,Passage: doc.data().Passage,isPassageQuestion: doc.data().isPassageQuestion,});
console.log("English Questions: ",doc.id);
});
console.log("Done copying the English database");
});
}
解决方法
请记住,许多Firestore调用会返回一个 promise -这意味着操作的结果将在以后(可能在很多之后)返回。仅仅在一个电话上接一个电话并不意味着一个电话会等待另一个电话-这根本不是诺言所能做的。您可能已经看到一些使用异步/等待的代码,它们像彼此等待一样 LOOK -但这只是“语法糖”(即看起来很不错),隐藏了 STILL 承诺“在幕后”的事实。
对于您的示例,我将执行以下操作:
copyMathDatabase() {
let db = fire.firestore();
return db.collection("Questions")
...
}
copyEnglishDatabase() {
let db = fire.firestore();
return db.collection("EnglishQuestions")
...
}
请注意“返回”-现在每个函数都返回结果的 PROMISE ,这将在以后的某个时间发生。然后,在您的身份验证功能区域:
fire
.auth()
.createUserWithEmailAndPassword(this.state.email,this.state.password)
.then((u) => {
let user = fire.auth().currentUser;
console.log(user);
if (user != null) {
user
.updateProfile({
displayName: this.state.name,})
.then((r) => {
let db = fire.firestore();
let data = {
name: this.state.name,email: this.state.email,college: this.state.college,dzongkhag: this.state.dzongkhag,};
return db.collection(this.state.email).doc("UserProfile").set(data);
})
.then(() => {
return this.copyMathDatabase();
})
.then(() => {
return this.copyEnglishDatabase();
};
}
});
}
note-.set()的结果返回一个 PROMISE ;完成后; .then()等待该承诺,然后 then 执行copyMathDatabase(),该操作完成后将返回 PROMISE 完成; .then()等待该承诺,然后 then 执行copyEnglishDatabase(),该操作完成后将返回 PROMISE 完成。
此问题表明您对诺言到底是什么几乎一无所知-而且对使用Firestore而言,这一理解绝对至关重要且必要。在继续之前,您需要详细地教自己的诺言。您还应该了解aync / await语法是什么,以及实际上的作用。这不是您学习这些东西的地方。
,这很可能是由于调用kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.3")
和copyMathDatabase()
(它们都是异步的)引起的同步问题,请尝试将代码块更改为以下内容:
copyEnglishDatabase()
通过这种方式,您可以强制代码以正确的顺序执行并减少错误,这应该可以解决您面临的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。