这是我的代码。成功返回false
因为文档长度为0。
var sucess = await Firestore.instance.runTransaction((transaction) async {
await transaction.set(userReference, userMap);
}).then((_) {
return Firestore.instance.collection(CollectionName.user).where(FirebaseUserField.uid, isEqualTo: userMap["uid"]).getDocuments();
}).then((querySnapshot) {
if (querySnapshot.documents.length == 0) {
return false;
}
return true;
}).catchError((e) {
print(e);
return false;
});
这段代码进入我的第二个未来,我在那里查询数据库中的用户,我刚用先前的事务更新过。 我希望用期货链接所有内容,以便简化错误检查。
从Firestore文档我可以看到处理程序解析为,我认为,返回事务的值,但调用事务的实例解析为空映射。
runTransaction((Transaction) → Future<dynamic> transactionHandler, {Duration timeout: const Duration(seconds: 5)}) → Future<Map<String, dynamic>>
在交易完成后调用下一个未来的正确方法是什么,或者如果失败则会出错。
要考虑的一件事是,当我使用断点调试我查询用户时,所以有一个延迟,我得到了正确的用户。
此外,用户已正确写入我的数据库。
编辑:我也试过var sucess = await Firestore.instance.runTransaction((transaction) async {
return transaction.set(userReference, userMap);
})
但它没有用。
答案 0 :(得分:0)
我认为它与您事务中的嵌套await
有关,这也可以解释您在调试期间看到的竞争条件。
在runTransaction
中使用此事务处理程序时会发生什么?
(transaction) {
return transaction.set(userReference, userMap);
}
答案 1 :(得分:0)
你正在混合期货等待。尝试在1 <?php
2
3 $conn = mysqli_connect("localhost","root","","project");
4 if(!$conn)
5 die("Connection failed" . mysqli_connect_error());
6
7 $descr = $_POST['descr'];
8 $acecode = $_POST['acecode'];
9 $acename = $_POST['acename'];
10 $acemail = $_POST['acemail'];
11 $status = $_POST['status'];
12 $comments = $_POST['comments'];
13
14 //check for acemail
15 if($acecode == NULL and $acename == NULL and $acemail != NULL)
16 {
17 $search = "SELECT * from empmaster where empmail=".$acemail;
18 $result = mysqli_query($conn,$search);
19 if(mysqli_num_rows($result) == 1)
20 {
21 while($row = mysqli_fetch_assoc($result))
22 {
23 $acecode = $row['empcode'];
24 $acename = $row['empname'];
25 }
26 }
27
28 else if(mysqli_num_rows($result) == 0)
29 {
30 echo "No such record exists";
31 $acemail = NULL;
32 }
33 else
34 {
35 echo 'Conflicting values found in Table "empmaster".';
36 $acemail = NULL;
37 }
38 }
39
40 //check for acecode
41 else if($acename == NULL and $acemail == NULL and $acecode != NULL)
42 {
43 $search = "SELECT * from empmaster where empcode=".$acecode;
44 $result = mysqli_query($conn,$search);
45 if(mysqli_num_rows($result) == 1)
46 {
47 while($row = mysqli_fetch_assoc($result))
48 {
49 $acemail = $row['empmail'];
50 $acename = $row['empname'];
51 }
52 }
53
54 else if(mysqli_num_rows($result) == 0)
55 {
56 echo "No such record exists";
57 $acecode = NULL;
58 }
59 else
60 {
61 echo 'Conflicting values found in Table "empmaster".';
62 $acecode = NULL;
63 }
64 }
65
66 //check for acename
67 else if($acecode == NULL and $acemail == NULL and $acename != NULL)
68 {
69 $search = "SELECT * from empmaster where empname like ".$acename;
70 $result = mysqli_query($conn,$search);
71 if(mysqli_num_rows($result) == 1)
72 {
73 while($row = mysqli_fetch_assoc($result))
74 {
75 $acecode = $row['empcode'];
76 $acemail = $row['empmail'];
77 }
78 }
79
80 else if(mysqli_num_rows($result) == 0)
81 {
82 echo "No such record exists";
83 $acename = NULL;
84 }
85 else
86 {
87 echo 'Conflicting values found in Table "empmaster".';
88 $acename = NULL;
89 }
90 }
91
92 //last condition
93 else
94 {
95 $acecode=NULL;
96 $acename=NULL;
97 $acemail=NULL;
98 echo "Input only one value of Task Actionee";
99 }
100
101 echo $acecode."<br>".$acename."<br>".$acemail;
102 ?>
Firestore.instance.runTransaction
您可能还需要从var sucess = Firestore.instance.runTransaction((transaction) async {
await transaction.set(userReference, userMap);
}).then((_) {
return Firestore.instance.collection(CollectionName.user).where(FirebaseUserField.uid, isEqualTo: userMap["uid"]).getDocuments();
}).then((querySnapshot) {
if (querySnapshot.documents.length == 0) {
return false;
}
return true;
}).catchError((e) {
print(e);
return false;
});
移除await
,因为这将立即返回,但不是100%确定