首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有一种方法可以创建Auth对象并使用该UID来创建一个带有GeoFirestore文档

有没有一种方法可以创建Auth对象并使用该UID来创建一个带有GeoFirestore文档
EN

Stack Overflow用户
提问于 2019-12-11 21:29:31
回答 2查看 70关注 0票数 0

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

代码语言:javascript
复制
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)引用功能。我如何才能做到这一点。

EN

回答 2

Stack Overflow用户

发布于 2019-12-11 22:16:39

你需要做的是

代码语言:javascript
复制
await storesCollection.doc(user.uid).set({...})

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

add()方法是GeoCollectionReference的一种方法。

票数 3
EN

Stack Overflow用户

发布于 2019-12-11 23:19:50

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

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

代码语言:javascript
复制
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: []
  });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59286903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档