【问题标题】:The development server returned response error code: 500 in react native when connect to firebase by installing firebase开发服务器返回响应错误代码:通过安装firebase连接到firebase时反应本机500
【发布时间】:2021-12-18 11:18:04
【问题描述】:

我尝试通过react native 连接到firebase。 这是代码

import React, {Component} from 'react'
import {View} from 'react-native'
import Card from './card'
import { initializeApp } from 'firebase/app'
import firebase from 'firebase/compat/app'


const firebaseConfig = {
  apiKey: "",
  authDomain: "tinderclone-99d8e.firebaseapp.com",
  projectId: "tinderclone-99d8e",
  storageBucket: "tinderclone-99d8e.appspot.com",
  messagingSenderId: "264605926507",
  appId: "1:264605926507:web:c9309a6669322ef6839d2e",
  databaseURL:''
  
};


firebase.initializeApp(firebaseConfig)

export default class App extends Component {

  state = {
    profileIndex: 0,
  }

  componentWillMount() {
    firebase.database().ref().child('users').once('value', (snap) => {
      console.log('Data', snap.val())
    })
  }


  nextCard = () => {
    this.setState({profileIndex: this.state.profileIndex + 1})
  }

  render() {
    const {profileIndex} = this.state
    return (
      <View style={{flex:1}}>
        {profiles.slice(profileIndex, profileIndex + 3).reverse().map((profile) => {
          return (
            <Card
              key={profile.id}
              profile={profile}
              onSwipeOff={this.nextCard}
            />
          )
        })}
      </View>
    )
  }
}

const profiles = [
  {
    id: '259389830744794',
    name: 'Candice',
    birthday: '10/18/1986',
    bio: 'Supermodel',
  },
  {
    id: '720115413',
    name: 'Alessandra',
    birthday: '1/10/1989',
    bio: 'Dancer',
  },
  {
    id: '169571172540',
    name: 'Miranda',
    birthday: '12/12/1983',
    bio: 'Doctor',
  },
  {
    id: '1476279359358140',
    name: 'Alissa',
    birthday: '2/11/1990',
    bio: 'Comedian',
  },
  {
    id: '1140849052644433',
    name: 'Behati',
    birthday: '3/23/1991',
    bio: 'Developer',
  },
  {
    id: '912478262117011',
    name: 'Rosie',
    birthday: '9/4/1989',
    bio: 'Artist',
  },
  {
    id: '173567062703796',
    name: 'Kendall',
    birthday: '8/17/1992',
    bio: 'Truck Driver',
  },
]

这里是数据库

这里有错误

While trying to resolve module `firebase` from file `C:\Users\PC\Desktop\de\App.js`, the package `C:\Users\PC\Desktop\de\node_modules\firebase\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\PC\Desktop\de\node_modules\firebase\index`. Indeed, none of these files exist:

我已经通过npm 安装了firebase 我尝试run npm install 和 npm 通过搜索其他答案重新开始 他们的回答对我不起作用。 2021年,firebase和以前不一样了。 我不知道如何解决这个问题 有人可以帮我吗

【问题讨论】:

    标签: android reactjs firebase react-native


    【解决方案1】:

    您在文件顶部导入import * as firebase from 'firebase'..尝试导入此行并告诉我它是否有效。 import firebase from "firebase/compat/app";

    firebase 的某些路径在很久以前就已更改,因此如果您正在观看旧代码片段,您可能会遇到错误。

    【讨论】:

    • 谢谢,这是正确的导入。之后我从'firebase/app'导入导入{initializeApp}。当渲染显示错误为'app2.default.database is not function.need to import this database ?
    • 如果您也使用 firebase auth 和 firestore,请尝试导入 import "firebase/compat/auth"import "firebase/compat/firestore"
    • 我没有使用身份验证和firestore。错误指向componentWillMount() { firebase.database().ref().child('users').once('value', (snap) =&gt; { console.log('Data', snap.val()) }) }
    • 我还检查了文档,使用 firebase.database().ref() 并没有什么特别之处。
    • import import database from '@react-native-firebase/database' 如果你使用的是 react-native firebase。并且可能需要上面的导入来改变
    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2018-02-10
    • 2017-06-07
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多