Redux(React)と、Firestoreを使ってユーザーと、それに関連したプロフィール情報を取得して表示しようと考えています。コンソール上では、いずれの値も確認できているのですがなぜか表示しようとすると下記のようなエラーが生じてしまいます。
Cannot read property 'content' of undefined
Firestoreでは、users
と、それに関連したprofiles
コレクションをそれぞれ引っ張ってきます。
action部分
Javascript
1export function fetchUser(id) { 2 return dispatch => { 3 usersRef 4 .doc(id) 5 .get() 6 .then(doc => { 7 profilesRef 8 .where("uid", "==", id) 9 .where("status", "==", doc.data().current_status) 10 .get() 11 .then(function(querySnapshot) { 12 querySnapshot.forEach(function(snapshot) { 13 const profileItems = { ...doc.data(), profiles: snapshot.data() }; 14 dispatch({ 15 type: FETCH_USER, 16 payload: profileItems 17 }); 18 }); 19 }); 20 }); 21 }; 22}
reducer部分
JavaScript
1import { FETCH_USER } from "../actions"; 2 3const initialState = {}; 4 5export default (state = initialState, action) => { 6 switch (action.type) { 7 console.log("action", action.payload) 8 // ここではUserrも、Profileもちゃんと取れてきているのが確認できている 9 case FETCH_USER: 10 return action.payload; 11 12 default: 13 return state; 14 } 15};
roooot reducer部分
Javascript
1import { createStore, applyMiddleware, combineReducers } from "redux"; 2import UsersReducer from "./reducer_users.js"; 3 4const rootReducer = combineReducers({ 5 users: UsersReducer, 6}); 7 8const store = createStore(rootReducer, applyMiddleware(reduxThunk)); 9export default store;
● Profile部分は取れてきている。
{content: "Hello world", status: 2}
container部分
JavaScript
1function mapStateToProps({ users }) { 2 console.log("content", user.profiles.content) 3 return { user: users, profiles: user.profiles }; 4}
ここにきて、冒頭に書いたUndefinedエラーとなってしまっています。そもそも、Firestoreで取り出した時にsnapshooot.data()
が配列だから取り出せないのかとも思ったのですが、user.profiles
までは正常に●部分のように表示されています。
強引に、Json化してしまおうと思い、
JavaScript
1 const profiles_json = JSON.stringify(users.profiles); 2 console.log("test", JSON.parse(profiles_json));
を試してみましたが、Reactではそれは使えないと怒られてしまいました。
あなたの回答
tips
プレビュー