React-redux-firebase填充不起作用

时间:2019-11-21 12:03:01

标签: firebase react-native react-redux-firebase

我尝试使用populate将数据中的ID替换为Firebase中的其他数据,但不起作用。

我使用“ firebase”:“ ^ 7.3.0”,“ react-redux-firebase”:“ ^ 3.0.5”,“ redux”:“ ^ 4.0.4”,“ react-redux”:“ ^ 7.1.3“,” redux-thunk“:” ^ 2.3.0“。

我的Firebase数据库 click to show

App.js

import React, { Component } from 'react';
import AppContainer from './config/routes';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import rootReducer from './store/reducers/rootReducer';
import thunk from 'redux-thunk';
import { ReactReduxFirebaseProvider, getFirebase } from 'react-redux-firebase';
import { firebase} from './config/firebase';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk.withExtraArgument(getFirebase)),
);

const rrfConfig = {
 userProfile: 'users',
 profileParamsToPopulate: [
   { child: 'nutrient', root: 'nutrients_list' } 
 ]
}

const reactReduxFirebaseProps = {
  firebase,
  config: rrfConfig ,
  dispatch: store.dispatch,
};

export default class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <ReactReduxFirebaseProvider {...reactReduxFirebaseProps}>
          <AppContainer />
        </ReactReduxFirebaseProvider>
      </Provider>
    );
  }
}

config.js

import firebase from 'firebase/app';
import 'firebase/database'

const config = {
  apiKey: 'my apiKey'
  authDomain: 'my authDomain',
  databaseURL: 'my databaseURL',
  projectId: 'my projectId',
  storageBucket: 'my storageBucket',
  messagingSenderId: 'my messagingSenderId',
  appId: 'my appId',
  measurementId: 'my measurementId',
};

if (!firebase.apps.length) {
  firebase.initializeApp(config);
}

export { firebase }

Food.js(在此组件中,我使用填充)

import React, { Component } from 'react';
import { View, Text, StyleSheet, ScrollView } from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { firebaseConnect, populate, isLoaded, isEmpty } from 'react-redux-firebase';

class Food extends Component {
render() {
    const { mealsArray } = this.props;

    if (!isLoaded(mealsArray)) {
      return <ActivityIndicator size="large" />
    }

    if (isEmpty(mealsArray)) {
      return <Text>There is no meals </Text>
    }

    return (
      <View>
        <ScrollView>
          <Text>{JSON.stringify(mealsArray, null, 2)}</Text>
        </ScrollView>
      </View>
    );
  }

const populates = [
  { child: 'nutrient', root: 'nutrients_list' }
]

const mapStateToProps = state => {
    return {
    mealsArray: populate(state.firebase, 'meals', populates)
  };
};

export default compose(
  firebaseConnect([
    { path: 'meals', queryParams: ['orderByChild=date', 'equalTo=18/11/2019'], populates},
  ]),
  connect(
    mapStateToProps,
  ),
)(Food);

我希望键“营养”将返回对象而不是ID号:

{
  nutrient: {
    name: 'buckwheat',
    proteins: 12.6,
    fats: 2.6,
    carbs: 68,
    calories: 329,
    water: 10,
  },
  number: 1,
  weight: 100,
}

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试在一个列表(餐)中的每个项目中填充一个项目(营养物)列表,深度为两个级别。 preg_replace('/[http://].*\.(css)/i','www.new-domain.net',$Html_contents) 当前不支持多层次的深层人口,您最好每顿饭都使用单独的成分。然后,在每个进餐成分中,您可以遍历所有营养素并填充每种营养素。

相反,最好是为每种营养物存储唯一的键,然后在另一种键(例如populatenutrients_amounts

它还将使用nutrients_meta生成的唯一键作为项目键,而不是数组索引。这样可以更轻松地在数据库的另一部分中引用值,并有助于避免出现稀疏数组的情况。