简体   繁体   English

如何将EXPO应用程序连接到Google Firestore?

[英]How to connect an expo app to google firestore?

I am trying to authenticate a user and also organize the users in my database by their uid and then have the data field be by email. 我正在尝试对用户进行身份验证,并通过其uid在我的数据库中对用户进行组织,然后通过电子邮件发送数据字段。 The users are successfully being authenticated however nothing appears in my firestore database. 用户已成功通过身份验证,但是在我的Firestore数据库中没有任何显示。

My firestore security rules are in test mode so there should be no issue with permissions. 我的Firestore安全规则处于测试模式,因此权限应该没有问题。 In my action dispatch, I am successfully getting the user from firebase with their uid, access token, and everything else. 在我的动作调度中,我已经成功地将用户的uid,访问令牌以及其他所有内容从firebase中引出。 I believe the error must be some way I am trying to connect to the firestore. 我认为该错误一定是我尝试连接到Firestore的某种方式。

In my app.js file I initialize firebase like this- 在我的app.js文件中,我像这样初始化firebase-

import firebase from 'firebase';
import '@firebase/firestore';

componentDidMount() {
    const firebaseConfig = {
      apiKey: 'xxxxxxxx',
      authDomain: 'xxxxxxxxx.firebaseapp.com',
      databaseURL: 'xxxxxxxx.firebaseio.com',
      projectId: 'xxxxxxxx',
      storageBucket: 'xxxxxxx.appspot.com',
      messagingSenderId: 'xxxxxxxxxxx',
      appId: 'xxxxxxxxxxxxxxxxxxxx'
    };
    firebase.initializeApp(firebaseConfig);
  }

Then this is how I am trying to make the call to put the user into the database- 然后,这就是我试图拨打电话将用户放入数据库的方式-

firebase.auth().createUserWithEmailAndPassword(email, password)
   .then((user) =>
     dispatch({ type: USER_LOGIN_SUCCESS, payload: user });
     const dbRoot = firebase.firestore();
     dbRoot.collection('users').doc(user.user.uid).set({ email });
   });

Also at the top of that file, I am importing as follows 同样在该文件的顶部,我按如下方式导入

import firebase from 'firebase';
import '@firebase/firestore';

Again, these users are being successfully authenticated but they are not appearing in the firestore database. 同样,这些用户已成功通过身份验证,但未出现在Firestore数据库中。

You have to add a collection to the database and add data . 您必须向database添加一个collection并添加data

const dbRoot = firebase.firestore().collection('users');
...
addData() {
  dbRoot.add({
    user: user.user.uid,
    email : email,
    complete: true,
  });

OR 要么

const dbRoot = firebase.firestore().collection('users');
...
 const defaultDoc = {
        email: email
      };

async addData() {
  await  dbRoot.doc(user.user.uid).set(defaultDoc);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM