【问题标题】:Is there a way to create Auth object and use that UID to create a doc with GeoFirestore有没有办法创建 Auth 对象并使用该 UID 使用 GeoFirestore 创建文档
【发布时间】:2019-12-11 13:29:31
【问题描述】:

我正在尝试在 firebase 中创建一个返回用户 UID 的 Auth 对象。我希望能够使用该特定 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) 引用功能。我怎样才能做到这一点。

【问题讨论】:

  • dispatch({ type: LOGIN, payload: { ...storesCollection.doc(user.uid) } }); 行没有意义。有效负载是否意味着存储在此文档中的数据?目前,此处的有效负载将是 GeoDocumentReference 的不良副本,而不是该文档的数据。

标签: firebase react-native google-cloud-firestore geofirestore


【解决方案1】:

你需要做的

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

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

add() 方法是GeoCollectionReference 的方法。

【讨论】:

    【解决方案2】:

    因为 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: []
      });
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多