我的控制台输出正在生成正确的对象,然后在阵列图的末尾崩溃,我无法弄清楚undefined
是什么。这是我的reducer,action和list实现。
减速器:
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case LOCATION_FETCH_SUCCESS:
return action.payload;
case POSTS_FETCH_FAIL:
return state;
default:
return state;
}
};
动作创建者:
export const locationFetch = () => {
const geoFire = new GeoFire(firebase.database().ref('/geoLocations'));
var keys = [];
var snaps = [];
return (dispatch) => {
navigator.geolocation.getCurrentPosition(
(position) => {
const currentLocation = [position.coords.latitude, position.coords.longitude];
const radius = 0.5;
const geoQuery = geoFire.query({ center: currentLocation, radius });
var onKeyEntered = geoQuery.on("key_entered", (key) => {
keys.push(key);
console.log(key);
});
var onKeyReady = geoQuery.on('ready', () => {
keys.map((key) => {
firebase.database().ref('/posts').child(key).
once('value').then((snapshot) => {
snaps.push(snapshot.val());
// console.log(snaps);
dispatch({ type: LOCATION_FETCH_SUCCESS, payload: snaps });
});//.catch(() => postsFetchFail(dispatch));
});
});
});
};
};
mapstatetoprops :
const mapStateToProps = state => {
var objectArray = _.values(state.posts)
objectArray = _.flatten(objectArray);
//console.log(objectArray);
objectArray.map((post) => {
const { dishname,
restaurant,
category,
likesCount
} = post;
console.log({ dishname , restaurant });
return { dishname, restaurant };
});
};
export default connect(mapStateToProps, {locationFetch})(DshdList);
控制台的最后一个输出和我得到的错误消息如下:
10:43:33 PM
Object {
"dishname": "shrimp bun",
"restaurant": "momofuku ",
}
10:43:38 PM
▼mapStateToProps() in Connect(DshdList) must return a plain object. Instead received undefined
我不知道这出错了。
答案 0 :(得分:0)
您必须从 mapStateToProps 函数返回对象,这是您缺少的。放置一个如下所示的退货声明
return objectArray.map(...);
希望这会有所帮助。