gpt4 book ai didi

javascript - 无法从 Firebase 保存或获取数据(React Native 和 Firebase 的新增功能)

转载 作者:行者123 更新时间:2023-12-03 01:06:36 25 4
gpt4 key购买 nike

所以我有一个日记应用程序,我正在尝试用 React Native 编写它。我是个新手,但我读到的所有内容都表明我所做的事情应该有效。基本上,我将一些数据放入 redux Action 类中并尝试编写它。但我不断得到,

未定义不是一个对象“评估'_firebase2.default.database.ref')

我已经仔细阅读了我多次使用的教程中的“将新数据保存到 firebase”部分,但我只是不明白我做错了什么。这是我的代码,

import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
import { DIARY_UPDATE, DIARY_SAVED, DIARY_FETCH_SUCCESS } from './types';


export const diaryUpdate = ({ prop, value }) => ({
type: DIARY_UPDATE,
payload: { prop, value }
});

export const diarySave = ({ date, location, activity, satisfaction,
amenities, note }) => {
const { currentUser } = firebase.auth();

return (dispatch) => {
firebase.datebase.ref(`Users/${currentUser.uid}/diary`)
.push({ date, location, activity, satisfaction, amenities, note })
.then(() => {
dispatch({ type: DIARY_SAVED });
Actions.dashboard({ type: 'Replace' });
});
};
};

export const diaryFetch = () => {
const { currentUser } = firebase.auth();

return (dispatch) => {
firebase.datebase.ref(`Users/${currentUser.uid}/diary`)
.on('value', snapshot => {
dispatch({ type: DIARY_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};

我有控制台记录这些值,以检查传递的内容是否有问题。

这是我正在初始化 firebase 的 App.js。我应该提到 firebase 的身份验证功能运行良好!

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import firebase from 'firebase';
import reduxThunk from 'redux-thunk';
import reducers from './src/reducers';
import Router from './src/Router';

export default class App extends Component {

componentWillMount() {
if (!firebase.apps.length) {
const fconfig = {
apiKey: 'AIzaSyAkYD2TodRFEiln8JZPp2DtDyfIwIr654U',
authDomain: 'yourhappyplace-6c049.firebaseapp.com',
databaseURL: 'https://yourhappyplace-6c049.firebaseio.com',
projectId: 'yourhappyplace-6c049',
storageBucket: 'yourhappyplace-6c049.appspot.com',
messagingSenderId: '634967086167'
};
firebase.initializeApp(fconfig);
}
}

render() {
//Create redux store with redux thunk middleware
const happyStore = createStore(reducers, {}, applyMiddleware(reduxThunk));

return (
<Provider store={happyStore}>
<Router />
</Provider>
);
}
}

有人能看出我到底做错了什么吗?谢谢!

最佳答案

要访问 FirebaseDatabase 实例,请在 FirebaseApp 实例上调用 database() 方法

拼写为database()并加上括号:

firebase.database().ref(...)

关于javascript - 无法从 Firebase 保存或获取数据(React Native 和 Firebase 的新增功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52369856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com