Reactjs 对firestore进行本机响应:firestore(8.2.1):连接网络通道传输错误

Reactjs 对firestore进行本机响应:firestore(8.2.1):连接网络通道传输错误,reactjs,firebase,react-native,google-cloud-firestore,Reactjs,Firebase,React Native,Google Cloud Firestore,我创建了简单的React原生屏幕,可以将数据存储到firestore。我尝试了下面的代码,但它没有工作,而是抛出了一些错误。有人能帮我吗 我的代码: App.js import React, { Component } from 'react'; import {StyleSheet,Text,View,TextInput,Button,TouchableHighlight} from 'react-native'; import firebase from 'firebase'; impor

我创建了简单的React原生屏幕,可以将数据存储到firestore。我尝试了下面的代码,但它没有工作,而是抛出了一些错误。有人能帮我吗

我的代码:

App.js

import React, { Component } from 'react';
import {StyleSheet,Text,View,TextInput,Button,TouchableHighlight} from 'react-native';

import firebase from 'firebase';
import firestore from '@react-native-firebase/firestore';

const firebaseConfig = {
  apiKey: "*********************",
  authDomain: "test-c78ec.firebaseapp.com",
  projectId: "test-c78ec",
  storageBucket: "test-c78ec.appspot.com",
  messagingSenderId: "106189113329",
  appId: "1:106189113329:web:4bf80ec51eba69ab042650",
  measurementId: "G-875ZSQLZS4"
};
firebase.initializeApp(firebaseConfig);

 export default class App extend components{

check2(){
  console.log("level strarted");
  firebase
  .firestore()
  .collection("MyCollection")
  .doc("mydoc")
  .set({
    key: "2",
    value: "World",
  })
  .then((ref) => { console.log(ref);
    console.log("sucessssssssssssssss")
   });
}

render(){
 return(
  <View>
    <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={() =>    this.check2('login')}>
          <Text style={styles.loginText}>Store data</Text>
        </TouchableHighlight>
   </View>
  );
 }
}
期望值:


只需将给定的数据写入firestore数据库。

在做了大量工作之后,我找到了一个解决该错误的临时方法。解决方案是在firebase初始化后添加以下代码。我不知道这是正确的解决方案,但现在效果很好

firebase.initializeApp(firebaseConfig);

firebase.firestore().settings({ experimentalForceLongPolling: true }); //add this..
在实施Firestore云功能(触发器)时,我使用的是基于常规/React管理的应用程序,我也看到了这个问题。有一段时间,我有点被关于a的建议误导了,这些建议是关于将
实验或长期轮询
标志设置为
true
。那个变通办法对我不起作用

相反,我意识到原因很简单(更像是我犯了一个愚蠢的错误):我的触发器注册了一个格式不正确的文档路径。也就是说,我有一个云函数,看起来像这样:

exports.onCreateFruitStand = functions.firestore
.document("vendors/{vendorId}/stands{standId}")
.onCreate(async (change, context) => {
  console.log(`vendors.stands.onCreate: vendors/${context.params.vendorId}/stands/${context.params.standId}`);
  const data = change.data(); // grab the latest data
  ...
});
我的Firestore emulator没有响应任何正在创建的新文档。关键的实现是需要将文档路径更改为
“vendors/{vendorId}/stands/{standId}”
(注意缺少的
/
)。从某种意义上说,发送到客户端的错误消息具有误导性,Firestore函数应该只是说文档路径的格式不正确。

这对我来说很有效:

//Write this line below of firebase.initializeApp(firebaseConfig)

firebase.firestore().settings({ experimentalForceLongPolling: true });

这似乎与上面的例子非常接近。在我看来,您的数据没有正确发送到Firestore。
//Write this line below of firebase.initializeApp(firebaseConfig)

firebase.firestore().settings({ experimentalForceLongPolling: true });