首页 > 解决方案 > 无法将数据推送到 Firestore 模拟器

问题描述

我是火力基地的新手。而且我无法将数据推送到 Firestore 模拟器。下面是我用来推送数据的代码。'SaveFireStore' 是应该向模拟器发送数据的功能。我正在使用 iPhone 模拟器。

import {View, Button} from 'react-native';
import firebase from '@react-native-firebase/app';
import firestore from '@react-native-firebase/firestore';
// Initialize Firebase
const config = {
  apiKey: 'KEY',
  authDomain: '',
  databaseURL: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
};
if (!firebase.apps.length) {
  firebase.initializeApp(config);
  const db = firestore();
  db.settings({host: 'localhost:8080', ssl: false});
} else {
  firebase.app(); // if already initialized, use that one
}

const App = () => {
  const SaveFireStore = async () => {
    const result = await firestore().collection('testing').doc().set({});
    console.log('Result = ', result);
  };
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Button title="press" onPress={() => SaveFireStore()} />
    </View>
  );
};

export default App;```

标签: androidreactjsfirebasereact-native

解决方案


我能够将数据推送到 Firestore 模拟器。我在 firebase 上创建了一个 android 项目(不是 web 项目)。在设置集合中的数据之前,我刚刚添加了以下行。

firestore().settings({ host: "localhost:8080", ssl: false });

注意主机字符串它不是'http://localhost:8080'。如果我尝试使用 http://localhost:8080 那么应用程序崩溃了。


推荐阅读