【问题标题】:Using Flow - How to use DocumentRef to check type of DataSnapshot fields?使用 Flow - 如何使用 DocumentRef 检查 DataSnapshot 字段的类型?
【发布时间】:2018-07-13 05:40:57
【问题描述】:

我创建了一个函数来遍历 DataSnapshot 的所有字段。

这是我的导入

import type {
  DataSnapshot,
  DocumentRef,
  DocumentSnapshot,
} from 'react-native-firebase';

函数如下:

getRefData = (data: typeof DataSnapshot) {
   return new Promise(async (resolve: Function) =>{
      const refs = await Object.keys(data).map((key: string) => {
         const field = data[key];
         if (field instanceof DocumentRef) {
             /// LOGIC HERE
         }
      });

   });
};

如果字段是DocumentRef,我实际上想添加一些逻辑。它甚至是未定义的,请参见下面的错误:

Unhandled rejection is {promise: Promise, reason: ReferenceError: DocumentRef is not defined
    at blob:http://localhost:8081/68a9c3b0-2327-429a-b5c7…}

有没有不同的或直接的方法来做到这一点?出了什么问题?

【问题讨论】:

  • javascript 不会看到任何作为类型导入的内容。所有类型的导入和信息都被完全剥离。您需要根据其中的信息确定字段的类型,而不是检查其类型。查看此示例:babeljs.io/repl/…

标签: javascript google-cloud-firestore flowtype react-native-firebase


【解决方案1】:

我认为在 JavaScript 中还没有直接的方法可以做到这一点,因为很难使用类型进行验证,所以我决定通过添加名为 refsobject 来更改我的 Firestore 结构这将包含所有引用,这将使我更容易遍历所有引用,因为我确信refs 字段下的所有内容都是DocumentRefs。

【讨论】:

    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2018-12-31
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多