有没有办法创建身份验证对象并使用该 UID 通过 GeoFirestore 创建文档



我正在尝试在Firebase中创建一个返回用户UID的身份验证对象。我希望能够使用该特定的 UID 在我的集合中创建一个文档,但显然 geofirestore 没有添加具有特定 ID 的文档的功能。

const storesCollection = geoFirestore.collection("retailers");
export const firstTimeStartCreateRetailer = ( email, password) => async dispatch => {
try {
const { user } = await auth.createUserWithEmailAndPassword(email, password);
await storesCollection.doc(user.uid).add({
coordinates: new firebase.firestore.GeoPoint(33.704381, 72.978839),
name: 'Freshlee',
location: 'F-11',
city: 'Islamabad',
inventory: [],
rating: 5,
categories: []
})
dispatch({ type: LOGIN, payload: { ...user } });
} catch (error) {
console.log(error)
}
};

此代码被拒绝,因为 geoFirestore 没有 .doc(id( 引用功能。我怎样才能做到这一点。

你需要做

await storesCollection.doc(user.uid).set({...})

使用set()方法。事实上,GeoDocumentReference没有add()方法,storesCollection.doc(user.uid)GeoDocumentReference

add()法是一种GeoCollectionReference法。

因为storesCollection是一个GeoCollectionReference,所以API并不总是与原生的Firestore引用相同。

在您的特定情况下,您可以使用doc(id)获取要写入的文档,但不是使用集合上使用的add(...),而是需要使用set(...)来创建/覆盖该特定文档的数据。

await storesCollection.doc(user.uid).set({
coordinates: new firebase.firestore.GeoPoint(33.704381, 72.978839),
name: 'Freshlee',
location: 'F-11',
city: 'Islamabad',
inventory: [],
rating: 5,
categories: []
});

最新更新