Javascript React,Redux:uncaughtreferenceerror:posts未定义
基本上,我有一个import语句,它接收保存在文件中的一些JSON数据,我现在从firebase读取这些数据,并试图将返回数据的值传递到defaultstate{},但收到上述错误消息。我如何解决这个问题Javascript React,Redux:uncaughtreferenceerror:posts未定义,javascript,reactjs,firebase,firebase-realtime-database,redux,Javascript,Reactjs,Firebase,Firebase Realtime Database,Redux,基本上,我有一个import语句,它接收保存在文件中的一些JSON数据,我现在从firebase读取这些数据,并试图将返回数据的值传递到defaultstate{},但收到上述错误消息。我如何解决这个问题 store.js import { createStore, compose, applyMiddleware } from 'redux'; import { syncHistoryWithStore } from 'react-router-redux'; import { browse
store.js
import { createStore, compose, applyMiddleware } from 'redux';
import { syncHistoryWithStore } from 'react-router-redux';
import { browserHistory } from 'react-router';
import thunk from 'redux-thunk';
import * as firebase from 'firebase';
// import the root reducer
import rootReducer from '../reducers/index';
//Firebase configuration details and initialisation
import { Firebase_Config } from '../config/config';
firebase.initializeApp(Firebase_Config);
const rootRef = firebase.database();
const refPosts = rootRef.ref('posts');
refPosts.on("value", snapshot => {
const posts = snapshot.val(); // I wish to pass this into defaultState{}
}, errorObject => {
console.log("The read failed: " + errorObject.code);
});
// Get initial state of all comments and posts
import comments from '../config/comments';
// import posts from '../config/posts';
// create an object for the default data
const defaultState = {
posts : posts, // I wish to pass in the snapshot value here
comments
};
大概
refPosts.on
是异步发生的,对吗?如果是这种情况,那么在创建defaultState时,POST将不存在。如果您改为创建一个空数组作为默认状态,然后在快照返回时调度一个操作来添加帖子,会怎么样?@azium来自Firebase的值事件确实是异步触发的。你能把你的评论写进一个答案吗?