arrays - 从 Firestore 检索的空数组中检索“getDocuments”查询中的数据
问题描述
我正在使用“getDocuments”查询从我的 Firestore 数据库中检索数据,但是我正在检索一个空数组。通过调试,我使用了a print(snapshot?.documents)
,发现返回了一个空数组。有人可以帮助我为什么要返回一个空数组吗?
获取通知功能:
public func getNotifications(
completion: @escaping ([IGNotification]) -> Void
) {
guard let username = UserDefaults.standard.string(forKey: "username") else {
completion([])
return
}
let ref = firestoreDatabase.collection("users").document(username).collection("notifications")
ref.getDocuments { snapshot, error in
guard let notifications = snapshot?.documents.compactMap({
IGNotification(with: $0.data())
}),
error == nil else {
completion([])
return
}
completion(notifications)
}
}
Firestore 数据:
解决方案
推荐阅读
- angular - 如何使用 createSpyObj 在 Angular 构造函数中模拟 Observable?
- python - 找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/about
- pandas - 转换数据框中的多个 dtype
- android - Android BLE:连接和订阅 2 个 BLE 外围设备的特征通知
- angular - 我必须在每个 api 调用中调用 gapi.load 吗?
- javascript - 如何在不同的html文件中注入jsx?
- python-asyncio - 限速 aiohttp.ClientSession() 每秒发出 N 个请求
- typescript - 使用 Typescript 和 cypress-select-tests 配置 Cypress
- python - 如何在py文件中引用这个id
- python - 将熊猫数据框转换为更小的部分