使用 Flow - 如何使用 DocumentRef 检查 DataSnapshot 字段的类型?
Using Flow - How to use DocumentRef to check type of DataSnapshot fields?
我创建了一个函数来遍历 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 中还没有直接的方法可以做到这一点,因为很难使用类型进行验证,所以我决定通过添加 object
named refs
将包含所有引用,这将使我更容易遍历所有引用,因为我确定 refs
字段下的所有内容都是 DocumentRef
s.
我创建了一个函数来遍历 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 中还没有直接的方法可以做到这一点,因为很难使用类型进行验证,所以我决定通过添加 object
named refs
将包含所有引用,这将使我更容易遍历所有引用,因为我确定 refs
字段下的所有内容都是 DocumentRef
s.