【问题标题】:React Native/Firebase Error app hasn't been createdReact Native/Firebase 错误应用尚未创建
【发布时间】:2017-10-28 19:04:09
【问题描述】:

问题

我正在创建一个简单的应用程序,人们可以在其中使用 react nativefirebase 创建帖子,当我添加所有代码以将变量“postInput”上传到一台服务器,我收到一个错误(包括图片)说该应用程序尚未创建,它告诉我调用App.initializeApp(),但我的应用程序中确实有该代码。我很想得到一些帮助来解决这个问题。

代码

import React, { Component } from 'react';
import { StyleSheet, Text, View, Image, TextInput, ScrollView, TouchableHighlight, Button } from 'react-native';
import { Font } from 'expo';
import * as firebase from 'firebase';

var fontLoaded = false;

var ref = firebase.database().ref('posts');

var newPostRef = ref.child("posts");

var newPostRef = postsRef.push();

const firebaseConfig = {
  apiKey: "AIzaSyD025SWUH7zLELn4vWtf9nGq1-0h33Y958",
  authDomain: "candidtwo.firebaseapp.com",
  databaseURL: "https://candidtwo.firebaseio.com",
  storageBucket: "candidtwo.appspot.com",
};
const firebaseApp = App.initializeApp(firebaseConfig); //tells me to call this, I do and still get error

export default class App extends React.Component {

  state = {
    fontLoaded: false,
  };



  componentDidMount() {
      Expo.Font.loadAsync({
        'JosefinSans-Regular.ttf': require('./JosefinSans-Regular.ttf'),
      });
 }
  constructor(props) {
    super(props);
    this.state = { postInput: ""}
  }

 render() {
    return (
      <View style={styles.container}>
        <View style={styles.button}>
          <View style={{width: 1, height: 30, backgroundColor: '#e8e8e8'}} />
          <Button
            onPress={() => this.setState({ fontLoaded: true })}
            title="Press Me To Load the App After 15 Seconds!"
            color="#fe8200"
            accessibilityLabel="Wait 15 seconds and then press me to load the font!"
          />
        </View>


        {this.state.fontLoaded ? (
          <View style={styles.container}>
            <Text style={{ fontFamily: 'JosefinSans-Regular', fontSize: 16 }}>
                Whats on your mind? Create a post!
            </Text>  

            <TextInput
                 style={{height:40, width: 320, borderColor: '#303030', borderWidth: 1}}
                 onChangeText={(postInput)=>this.setState({postInput})}
                 value={this.state.postInput}    
             />


    <Button
      onPress={() => {
        newPostRef.set({ content:this.state.postInput });
        this.setState({ postInput: "Your post was succsesfully uploaded! :)" })    
      }}               
      title="                              +                              "
      color="#fe8200"
    />

var path = newPostRef.toString();

            <ScrollView>
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 350, height: 250, backgroundColor: '#1daff1',  alignItems: 'center', justifyContent: 'center',    borderRadius: 10, paddingLeft: 10, paddingRight:10}} >
         <Text style={{ fontFamily: 'JosefinSans-Regular', fontSize: 18, color: '#ffffff', textAlign: 'center'}}>
                    Why do android phones have higher inital quality than apple phones, but apple phones have a more consistent amount of quality throughout their years?
                </Text>
            </View>
               <View style={{width: 350, height: 40, borderRadius: 10, backgroundColor: '#147aa8', flexDirection: 'row',paddingLeft:5}} >
            <Image source={require('./CandidtwoImages/unlove.png')} />
            <Text style={{ fontFamily: 'JosefinSans-Regular', fontSize: 18, color: '#ffffff'}}>
                    3
                    </Text>
            <Image source={require('./CandidtwoImages/undislike.png')} />
            <Text style={{ fontFamily: 'JosefinSans-Regular', fontSize: 18, color: '#ffffff'}}>
                    1
                    </Text>
            <Image source={require('./CandidtwoImages/comments.png')} />
            <Text style={{ fontFamily: 'JosefinSans-Regular', fontSize: 18, color: '#ffffff'}}>
                    8
                    </Text>
        </View>
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 35, height: 25, backgroundColor: '#147c41', borderRadius: 10}} />
               <View style={{width: 35, height: 4, backgroundColor: '#0f582d', borderRadius: 10}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
           <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 35, height: 25, backgroundColor: '#9dcd46', borderRadius: 10}} />
               <View style={{width: 35, height: 4, backgroundColor: '#6c8f31', borderRadius: 10}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
           <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />
               <View style={{width: 35, height: 25, backgroundColor: '#d3832e', borderRadius: 10}} />
               <View style={{width: 35, height: 4, backgroundColor: '#935b1e', borderRadius: 10}} />
               <View style={{width: 1, height: 6, backgroundColor: '#e8e8e8'}} />

             </ScrollView>
          </View>) : (null) }
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 8,
    backgroundColor: '#e8e8e8',
    alignItems: 'center'
  },
  button: {
    flex: 1,
    backgroundColor: '#e8e8e8',
    alignItems: 'center'
  },
});

错误信息

【问题讨论】:

    标签: javascript android firebase react-native firebase-realtime-database


    【解决方案1】:

    你应该调用它来初始化你的应用程序。

    const firebaseApp = firebase.initializeApp(firebaseConfig);
    

    【讨论】:

      【解决方案2】:

      所以,在这一行:

      var ref = firebase.database().ref('posts');
      

      当您的 firebase 应用尚未初始化时,您正尝试分配对您的 firebase 数据库的引用。

      这里:

      const firebaseApp = App.initializeApp(firebaseConfig);
      

      您正在尝试从您的 App 类调用 initializeApp() 方法来初始化 firebase 应用程序,并且此方法属于您在上面所做的 firebase 导入,您不需要初始化分配它的 firebase 应用程序到一个变量。

      你可以这样做:

      删除创建变量ref的行;

      像这样初始化你的 firebase 应用:

      firebase.initializeApp(config);
      

      然后你可以创建你的变量ref;

      var ref = firebase.database().ref('posts');
      

      【讨论】:

        猜你喜欢
        • 2016-11-15
        • 2017-12-03
        • 2020-05-12
        • 2023-01-20
        • 2020-12-24
        • 2018-12-17
        • 2018-11-13
        • 1970-01-01
        相关资源
        最近更新 更多