acces denied when going to write data database in firebase

2390 views react-native
-4

Problem:

I am creating an expo app with firebase integration. In there I have created a config file to connect with database. This is how it looks.

import Firebase from 'firebase';
 let config = {
    apiKey: "mykey",
    authDomain: "mytrain-5beba.firebaseapp.com",
    databaseURL: "https://mytain-5beba.firebaseio.com",
    projectId: "mytain-5beba",
    storageBucket: "mytain-5beba.appspot.com",
    messagingSenderId: "myid"
  };
let app = Firebase.initializeApp(config);
export const db = app.database();

In a services folder, I have created a service file like this to handle crud operations. This how it looks.

import { db } from '../config/db';

export const addItem = (item) => { db.ref('/tains').push({ name: item }); }

In my component In the componentDidMount method, I have done something like this to test weather database connection is working properly.

import React, { Component } from "react";
import {
  StyleSheet,
  Text,
  TextInput,
  View,
  TouchableOpacity,
  Dimensions,
  Picker,
  ListView
} from "react-native";

import {
  Ionicons,
  Foundation,
  Entypo,
  MaterialCommunityIcons,
  FontAwesome,
  MaterialIcons
} from "@expo/vector-icons";

import { Autocomplete } from "react-native-autocomplete-input";

import { addItem } from '../../services/stationService';

const { height, width } = Dimensions.get("window");
const box_width = width / 2 + 40;

export default class TicketForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: ""
    };
  }

  static navigationOptions = {
    title: "",
    headerStyle: {
      backgroundColor: "#2b78fe"
    },
    headerTintColor: "#fff",
    headerTitleStyle: {
      color: "#ffff"
    }
  };

  componentDidMount() {
    const name = "Tharindu";
    addItem(name);
  }




  render() {
    return (
      <View>
        <Text>Haiii</Text>
      </View>
    );
  }
}


});

When I run expo start --android. It showing me some warnings like this.

[15:52:56] [2018-11-05T10:22:51.070Z]  @firebase/database:, FIREBASE WARNING: set at /station/-LQYOMpnDzgdkj25XUDQ failed: permission_denied
- node_modules\react-native\Libraries\ReactNative\YellowBox.js:80:15 in warn
- node_modules\expo\src\Expo.js:26:41 in warn
- ... 20 more stack frames from framework internals

[15:53:01] [Unhandled promise rejection: Error: PERMISSION_DENIED: Permission denied]
- node_modules\@firebase\database\dist\index.cjs.js:13076:45 in <unknown>
- node_modules\@firebase\database\dist\index.cjs.js:690:8 in exceptionGuard
- ... 18 more stack frames from framework internals

[15:53:04] Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 360595ms)
- node_modules\react-native\Libraries\ReactNative\YellowBox.js:80:15 in warn
- node_modules\expo\src\Expo.js:26:41 in warn
- ... 11 more stack frames from framework internals

[15:59:03] Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 92226ms)
- node_modules\react-native\Libraries\ReactNative\YellowBox.js:80:15 in warn
- node_modules\expo\src\Expo.js:26:41 in warn
- ... 11 more stack frames from framework internals

I am very new to these react-native and firebase stuff.Can someone help me to solve this problem.Thank you!

answered question

1 Answer

2

Open firebase, select database on the left hand side. Now on the right hand side, select [Realtime database] from the dropdown and change the rules to:

{
"rules": {
".read": true,
".write": true
}
}

it workss..!!

posted this

Have an answer?

JD

Please login first before posting an answer.