Firestore返回True或False而不是Promise对象

时间:2020-08-08 22:49:28

标签: javascript firebase google-cloud-firestore promise async-await

因此,我具有将此功能存储在Firestore数据库中的功能,但是我想检查该值是否已经存在,并基于此值我想返回布尔值。 我一直试图通过异步等待来解决这个问题,但是它似乎没有用。最终,当我在Here is the full program import wx import matplotlib.pyplot as plt import networkx as nx from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas from networkx.drawing.nx_agraph import graphviz_layout class NetworkFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1) self.SetSize(wx.Size(1280, 768)) self.panel = wx.Panel(self) self.fig = plt.figure() self.canvas = FigCanvas(self.panel, -1, self.fig) G = nx.Graph() nodes = [0,1,2,3,4,5,6,7] node_sizes = [650,400,400,250,250,250,250,250] node_sizes = [x+1500 for x in node_sizes] node_color = ["#00d992","#00d9c8","#00d9c8","#00b4d9","#00b4d9","#00b4d9","#00b4d9","#00b4d9"] edges = [(1,0),(2,0),(3,1),(4,1),(5,1),(6,2),(7,2)] node_label = {0: "Printer", 1: "Case", 2: "Electronics", 3: "Plastic 1", 4: "Plastic 2", 5: "Plastic 3", 6: "Metal 1", 7: "Metal 2"} edge_weights = [1, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1, 0.1] # ????? new_edge_weights = [ int(x*10) for x in edge_weights] lengths = {} for e,l in zip(edges, new_edge_weights): lengths[e] = dict(len=l) G.add_nodes_from(nodes) G.add_edges_from(edges) nx.set_edge_attributes(G, lengths) pos = graphviz_layout(G, prog='neato') # Set the nodel label to empty for the new node node_label[len(node_label)+1] = "" # Set the size of the new node smaller than the master node master_idx = 0 node_sizes.append(node_sizes[master_idx]-300) # change the master node color to black node_color[master_idx] = "#000000" # set the color of the inner node (new smaller node) node_color.append("#00d992") # Set the position of the node same as the master node pos[len(pos)+1] = pos[master_idx] # Add node to the graph G.add_node(len(node_label)) # Draw the graph nx.draw_networkx(G, pos=pos, node_size = node_sizes, node_color = node_color, edge_weights='len', labels = node_label, with_labels=True) self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) self.panel.SetSizer(self.vbox) if __name__ == '__main__': app = wx.App() app.frame = NetworkFrame() app.frame.Show() app.MainLoop() 之前添加return时,它解决了问题。即使它解决了问题,我也不清楚原因。我知道它一定与异步有关。有人可以解释一下为什么添加退货可以解决问题吗?

performanceRef.get()

1 个答案:

答案 0 :(得分:0)

不是return解决了问题,异步/等待代码中可能存在一些问题,当您将其重写后进行回调时,它可以按预期工作。

如果您想获得更清晰的答案,请随时发布您的异步/等待版本。

P.S。您无需使用await关键字,因此在功能之前不需要async修饰符

根据评论更新

Await关键字只是帮助我们停止当前行上的函数执行并等待promise解决。如果您想获得promise的结果(在then回调中将其命名为doc),则需要将其存储在某个常量中:

const doc = await performanceRef.get()

因此您已经拥有了它,并且可以像然后在回调中一样执行任何类型的验证。

如果要从此函数返回验证结果,只需像以前一样使用return关键字即可。