4

EDIT - PROBLEM FIXED

Finally, after the samples of support received and the opinions, and doing countless tests, I decided to eliminate the project and start over. I can't say or explain what I was doing wrong or how the problem was corrected, I just started a project from scratch, and everything worked.

I must say that I already did everything shown in the following questions, without being able to bring the data from the Database to my application:

@firebase/firestore: Firestore (5.0.4): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds

React Native Could not reach Cloud Firestore backend

Ionic 5 emulator AVD Internet connection - error?

Firebase/Firestore not connected to internet

Firebase Firestore and Authentication Errors

Emulated firstore: Firestore (8.5.0): Could not reach Cloud Firestore backend

I have changed the version of Firebase, I have added code that works for others, I have looked for solutions on GitHub, etc. I also looked for a solution on this site, but I can't connect.

My React Native App makes use of Firebase. I am trying to join the Firestore DataBase to my application, with no success. The application so far has a list of products that are in the database, entered in it manually, from a Web application, which is perfectly connected to Firebase DataBase Making Debugger, I check that the fix reaches me, thanks to a console.log, but it is empty, and in the Database I have several elements entered manually.

I show an image from the console.log

I always get the same Warning:

>  @ firebase / firestore: Firestore (7.19.0): Could not reach Cloud
> Firestore backend. Backend didn't respond within 10 seconds. This
> typically indicates that your device does not have a healthy Internet
> connection at the moment. The client will operate in offline mode
> until it is able to successfully connect to the backend

They are already 10 days testing solutions. I have changed the version of Firebase, I have added code that works for others,I have looked for answers on GitHub, etc. I also looked for a solution on this site, but I can't connect.

I can't continue with my application if I can't get the data from the DataBase

I show images and code

import app from 'firebase/app'
import 'firebase/firestore'

import firebaseConfig from './config'

class Firebase {
    /*constructor() {
        if(!app.apps.length) {
            app.initializeApp(firebaseConfig)
        }

        this.db = app.firestore()
    }*/

    constructor() {
        if (!app.apps.length) {
            
            app.initializeApp(firebaseConfig)
            app.firestore().settings({ experimentalForceLongPolling: true })
            
        }

        this.db = app.firestore()
    }
}

const firebase = new Firebase()
export default firebase

FILE Menu.js

import React, { useContext, useEffect, Fragment } from 'react'
import { StyleSheet } from 'react-native'
import {
    Container,
    Separator,
    Content,
    List,
    ListItem,
    Thumbnail,
    Text,
    Body
} from 'native-base'
import globalStyles from '../styles/global'
import FirebaseContext from '../context/firebase/firebaseContext'

const Menu = () => {

    // Context de Firebase 
    const { menu, obtenerProductos } = useContext(FirebaseContext)

    useEffect(() => {
        obtenerProductos()

        console.log(menu)
    }, [])

    return (
        <Container style={globalStyles.contenedor}>
            <Content style={{ backgroundColor: '#FFF' }}>
                <List>
                    {menu.map(plato => {
                        const { imagen, nombre, descripcion, categoria, precio, id } = plato

                        return (
                            <Fragment key={id}>
                                <ListItem

                                >
                                    <Body>
                                        <Text>{nombre}</Text>
                                    </Body>
                                </ListItem>
                            </Fragment>
                        )
                    })}
                </List>
            </Content>
        </Container>
    )
}

const styles = StyleSheet.create({
    separador: {
        backgroundColor: '#000',
    },
    separadorTexto: {
        color: '#FFDA00',
        fontWeight: 'bold',
        textTransform: 'uppercase'
    }
})

export default Menu

FILE NuevaOrden.js

import React from 'react'
import { View, StyleSheet } from 'react-native'
import { Container, Button, Text, NativeBaseProvider } from 'native-base'
import globalStyles from '../styles/global'
import { useNavigation } from '@react-navigation/native'

const NuevaOrden = () => {

    const navigation = useNavigation()

    return (
        <Container style={globalStyles.contenedor}>
            <View style={[globalStyles.contenido, styles.contenido]}>
                <Button
                    style={globalStyles.boton}
                    rounded
                    block
                    onPress={() => navigation.navigate('Menu')}
                >
                    <Text style={globalStyles.botonTexto} >Crear Nueva Orden</Text>
                </Button>
            </View>
        </Container>
    )
}

const styles = StyleSheet.create({
    contenido: {
        flexDirection: 'column',
        justifyContent: 'center'
    }
})

export default NuevaOrden

FILE firebaseState.js

import React, { useReducer } from 'react'
import firebase from '../../firebase'
import FirebaseReducer from './firebaseReducer'
import FirebaseContext from './firebaseContext'

import { OBTENER_PRODUCTOS_EXITO } from '../../types'

const FirebaseState = props => {

  // Crear state inicial
  const initialState = {
    menu: []
  }

  // useReducer con dispatch  para ejecutar las funciones
  const [state, dispatch] = useReducer(FirebaseReducer, initialState)

  // Función que se ejecuta para traer los productos
  const obtenerProductos = () => {

    // consultar firebase
    //firebase.db.settings({ experimentalForceLongPolling: true })
    firebase.db
      .collection('productos')
      .where('existencia', '==', true) // traer solo los que esten en existencia
      .onSnapshot(manejarSnapshot)
      
    function manejarSnapshot(snapshot) {
      let platos = snapshot.docs.map(doc => {
        return {
          id: doc.id,
          ...doc.data()
        }
      })

      console.log(platos)

      // Tenemos resultados de la base de datos
      dispatch({
        type: OBTENER_PRODUCTOS_EXITO,
        payload: platos
      })    
    }
  }

  return (
    <FirebaseContext.Provider
      value={{
        menu: state.menu,
        firebase,
        obtenerProductos
      }}
    >
      {props.children}
    </FirebaseContext.Provider>
  )
}

export default FirebaseState

enter image description here

enter image description here

enter image description here

enter image description here

Estudiante
  • 92
  • 2
  • 18
  • 2
    I had such similar issues when one of my customers tried one of my apps. It worked on my side but noth on their side. We figured out that there IT department blocked lots of Google IPs. A small talk to them and some changes to the firewall and add blockers later and everything worked fine. Did you try it with another internet connection? – Tarik Huber Aug 05 '21 at 22:02
  • 1
    Thanks @TarikHuber , if I have tried another network and it still does not work – Estudiante Aug 07 '21 at 07:01
  • 1
    In past we realized an application with Firebase Web SDK and turns out on some of our user's computers the SDK could not reach firestore due to an antivirus that somewhat blocks the Google Firestore Endpoint. In our case we manage to make it work with the long poling setting described here https://firebase.google.com/docs/reference/js/firebase.firestore.Settings#optional-experimentalforcelongpolling – Mattia Galati Aug 08 '21 at 12:59
  • 2
    Have you set firestore rules in firebase console correctly? – I.Step Aug 14 '21 at 06:45
  • 1
    `rules_version= '2'; service cloud.firebase {match / databases/{database}/documents {match /{document=**} { allow read: if true; allow write} } }` Thakn you @I.Step , These are my rules, but they are well configured – Estudiante Aug 14 '21 at 09:46
  • 1
    For me, Kaspersky antivirus caused such problems. – Riwen Aug 14 '21 at 12:38

0 Answers0