【发布时间】: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