我的控制台输出正在生成正确的对象,然后在数组映射的末尾崩溃,我无法弄清楚undefined
是什么。这是我的化简器、操作和列表实现。
减速机:
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
我不明白这是哪里出了问题。
您必须从 mapStateToProps 函数返回对象,而您缺少该函数。放置如下所示的退货声明
返回对象数组.映射(...(;
希望这会有所帮助。