0

So I am here to ask if there is a method to use firebase with real-time database in 1 apps. My login already worked using Firestore.

This is my code that is storing 1 text to my Firestore data, Its worked too. I want to change it to real-time database whenever there a new data inserted, but still have the id user logged to my react-native app

The point is I want to use Realtime Database instead of Firestore. Login as Firestore, Save data as Realtime Database

import React, { useEffect, useState } from 'react'
import { FlatList, Keyboard, Text, TextInput, TouchableOpacity, View } from 'react-native'
import styles from './styles';
import { firebase } from '../../firebase/config'

export default function HomeScreen(props) {

    const [entityText, setEntityText] = useState('')
    const [entities, setEntities] = useState([])

    const entityRef = firebase.firestore().collection('entities')
    const userID = props.extraData.id

    useEffect(() => {
        entityRef
            .where("authorID", "==", userID)
            .orderBy('createdAt', 'desc')
            .onSnapshot(
                querySnapshot => {
                    const newEntities = []
                    querySnapshot.forEach(doc => {
                        const entity = doc.data()
                        entity.id = doc.id
                        newEntities.push(entity)
                    });
                    setEntities(newEntities)
                },
                error => {
                    console.log(error)
                }
            )
    }, [])

    const onAddButtonPress = () => {
        if (entityText && entityText.length > 0) {
            const timestamp = firebase.firestore.FieldValue.serverTimestamp();
            const data = {
                text: entityText,
                authorID: userID,
                createdAt: timestamp,
            };
            entityRef
                .add(data)
                .then(_doc => {
                    setEntityText('')
                    Keyboard.dismiss()
                })
                .catch((error) => {
                    alert(error)
                });
        }
    }

    const renderEntity = ({item, index}) => {
        return (
            <View style={styles.entityContainer}>
                <Text style={styles.entityText}>
                    {index}. {item.text}
                </Text>
            </View>
        )
    }

    return (
        <View style={styles.container}>
            <View style={styles.formContainer}>
                <TextInput
                    style={styles.input}
                    placeholder='Add new entity'
                    placeholderTextColor="#aaaaaa"
                    onChangeText={(text) => setEntityText(text)}
                    value={entityText}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TouchableOpacity style={styles.button} onPress={onAddButtonPress}>
                    <Text style={styles.buttonText}>Add</Text>
                </TouchableOpacity>
            </View>
            { entities && (
                <View style={styles.listContainer}>
                    <FlatList
                        data={entities}
                        renderItem={renderEntity}
                        keyExtractor={(item) => item.id}
                        removeClippedSubviews={true}
                    />
                </View>
            )}
        </View>
    )
}

Can someone help me and tell me how to do that? thanks

John C
  • 3,052
  • 3
  • 34
  • 47
Mdruid D
  • 49
  • 5
  • 2
    I'm not sure I understand your question, so will try to address some things: 1) users don't log in with Firestore or Realtime Database, but with Authentication. This is shared between two databases, so you shouldn't have to change anything there. 2) If you want to use Realtime Database instead of Firestore, I recommend starting with its documentation here: https://firebase.google.com/docs/database/web/start – Frank van Puffelen Sep 29 '20 at 23:51
  • sorry, i just so exiting to my project that make me in rush. First of all, i take number 2, thats my conditions. still confused about a little bit of documentation bcs i writing in react native and still new to this. But thanks ill read and wait someone give me a sample – Mdruid D Sep 29 '20 at 23:54
  • 1
    OK. So your question is whether a query on two fields is possible on Realtime Database? In that case: Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. In your case that could be `"authorID_createdAt": "MdruidDsID_1601429829422`. For a longer example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Sep 30 '20 at 01:37

0 Answers0