javascript - 用于同时保存文档和集合的结构 Firestore 查询
问题描述
我想为每个用户保存一个产品列表,并使用 geofirestore 查找最近用户的产品列表。
但是我将我的画笔与 Firestore 查询混合在一起。
import firestore from '@react-native-firebase/firestore';
import * as geofirestore from 'geofirestore';
const firestoreApp = firestore();
const GeoFirestore = geofirestore.initializeApp(firestoreApp);
const geocollection = GeoFirestore.collection('PRODUCTS');
geocollection
.doc(user.uid)
.set({
coordinates: new firestore.GeoPoint(
productLocation.lat,
productLocation.long,
),
})
.collection('USER_PRODUCTS')
.add({
name: productName,
description: productDescription,
price: productPrice,
quantity: productQuantity,
image: productImage.name,
createdDate: new Date(),
});
我只能使用用户 ID 和坐标设置第一个文档,但不能添加“USER_PRODUCTS”集合。
是否可以像这样链接,或者我必须进行两个不同的查询?
有人有更好的主意吗?
解决方案
我的解决方案是提出两个不同的请求(一个使用 Geofirestore,一个使用 Firestore)。我猜想并希望有一个只有一个查询的解决方案,但目前该解决方案有效。
await geocollection.doc(user.uid).set({
coordinates: new firestore.GeoPoint(
productLocation.lat,
productLocation.long,
),
});
await firestore()
.collection('PRODUCTS')
.doc(user.uid)
.collection('USER_PRODUCTS')
.doc()
.set(
{
name: productName,
description: productDescription,
price: productPrice,
quantity: productQuantity,
image: productImage.name,
createdDate: new Date(),
},
{merge: true},
);
推荐阅读
- xcode - Xcode 核心数据编辑器在 Xcode 12 中停止工作
- django - 如何在 Django / Graphene 测试中模拟 info.context
- typescript - 使用并发承诺填充记录
- ios - 在 iOS / Swift 中添加到现有的 UINavigationController
- swiftui - SwiftUI:有条件的 onDelete
- python - 变量以字符串形式出现;TypeError:abs()的错误操作数类型:'str'
- python - 使用列表更新嵌套字典时的奇怪行为
- scala - Scala 3 中还存在 volatile 类型吗?
- sql - 在 Firebird/SQLite 数据库中为字符串创建“校验和”
- ruby-on-rails - 如何摆脱用 Prawn 生成的 PDF 中奇怪的方框字符?