How to retrieve and load data from a Firestore subcollection using React-Redux-Firebase?
I want to fetch a document called settings
from a Firestore subcollection, then load it into my local component (and Redux store) as a variable named settings
with the following value:
settings: {
name: 'Waldo Garply',
email: 'corge@foogle.net',
mobile: '555-789-1234',
timestamp: '1546304499032',
}
Instead, my app fails to compile and I get the following error message in my console:
console.error
Uncaught TypeError: Cannot read property 'settings' of undefined at Function.mapStateToProps [as mapToProps]
What am I doing wrong and how can I achieve my expected behavior?
I am storing my Firestore data as follows.
Firestore
.
├── users
| ├── OGk02kJbQUesTeVhTrLBnERSxrfm
| | ├── settings
| | | ├── VrxDnSxpUw6wgX0n9c1FbapmLaLa
| | | | ├── name: Waldo Garply
| | | | └── timestamp: 1546304499030
| | | ├── cGVHxSkU3Lcb9WAYWjnJKcLOTYf8
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | └── timestamp: 1546304499031
| | | ├── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | ├── mobile: 555-789-1234
| | | | └── timestamp: 1546304499032
Notice, I am storing a unique snapshot of all the settings values (including a timestamp) each time any of the settings values changes; I then fetch the latest setting (sorted by timestamp) and load it. So I am needing an automatic listener on the settings
object.
I am using the following code in my component (called DetailsTab.js) to try connect to Firestore to fetch the data then load it as a settings
variable into my component and Redux store.
DetailsTab.js
function mapStateToProps( state ) {
console.log('staten', state);
return {
user: state.auth.user,
// attempted all the following individually
settings: state.firestore.data.users.settings, // throws error
settings: state.firestore.ordered.users.settings, // throws error
settings: state.firestore.ordered.users[0] // throws error
settings: state.firestore.ordered.users // returns 'users' object
}
}
export default compose(
withStyles(styles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect(props => {
return [
{
collection: 'users',
doc: props.user.data.uid,
subcollections: [
{
collection: 'settings',
limit: 1,
orderBy: ['timestamp', 'desc',],
storeAs: 'settings',
},
],
},
];
})
)(DetailsTab)
The following is how the data appears when my console logs it.
console.log
state
└── firestore
├── data
| └── users
| └── OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings
| └── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
├── ordered
| └── users [Array(1)]
| └── 0
| ├── id: OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings [Array(1)]
| └── 0
| ├── id: qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
reactjs react-redux react-redux-firebase
add a comment |
I want to fetch a document called settings
from a Firestore subcollection, then load it into my local component (and Redux store) as a variable named settings
with the following value:
settings: {
name: 'Waldo Garply',
email: 'corge@foogle.net',
mobile: '555-789-1234',
timestamp: '1546304499032',
}
Instead, my app fails to compile and I get the following error message in my console:
console.error
Uncaught TypeError: Cannot read property 'settings' of undefined at Function.mapStateToProps [as mapToProps]
What am I doing wrong and how can I achieve my expected behavior?
I am storing my Firestore data as follows.
Firestore
.
├── users
| ├── OGk02kJbQUesTeVhTrLBnERSxrfm
| | ├── settings
| | | ├── VrxDnSxpUw6wgX0n9c1FbapmLaLa
| | | | ├── name: Waldo Garply
| | | | └── timestamp: 1546304499030
| | | ├── cGVHxSkU3Lcb9WAYWjnJKcLOTYf8
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | └── timestamp: 1546304499031
| | | ├── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | ├── mobile: 555-789-1234
| | | | └── timestamp: 1546304499032
Notice, I am storing a unique snapshot of all the settings values (including a timestamp) each time any of the settings values changes; I then fetch the latest setting (sorted by timestamp) and load it. So I am needing an automatic listener on the settings
object.
I am using the following code in my component (called DetailsTab.js) to try connect to Firestore to fetch the data then load it as a settings
variable into my component and Redux store.
DetailsTab.js
function mapStateToProps( state ) {
console.log('staten', state);
return {
user: state.auth.user,
// attempted all the following individually
settings: state.firestore.data.users.settings, // throws error
settings: state.firestore.ordered.users.settings, // throws error
settings: state.firestore.ordered.users[0] // throws error
settings: state.firestore.ordered.users // returns 'users' object
}
}
export default compose(
withStyles(styles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect(props => {
return [
{
collection: 'users',
doc: props.user.data.uid,
subcollections: [
{
collection: 'settings',
limit: 1,
orderBy: ['timestamp', 'desc',],
storeAs: 'settings',
},
],
},
];
})
)(DetailsTab)
The following is how the data appears when my console logs it.
console.log
state
└── firestore
├── data
| └── users
| └── OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings
| └── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
├── ordered
| └── users [Array(1)]
| └── 0
| ├── id: OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings [Array(1)]
| └── 0
| ├── id: qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
reactjs react-redux react-redux-firebase
InmapStateToProps
try to fetch the data bystate.firestore.ordered.users.settings
to see if the data you are looking for is there.
– Dez
Jan 1 at 22:13
@Dez: That did not work. I edited the question to show all the possible paths tosettings
I have attempted undermapStateToProps
.
– Mowzer
Jan 1 at 23:36
add a comment |
I want to fetch a document called settings
from a Firestore subcollection, then load it into my local component (and Redux store) as a variable named settings
with the following value:
settings: {
name: 'Waldo Garply',
email: 'corge@foogle.net',
mobile: '555-789-1234',
timestamp: '1546304499032',
}
Instead, my app fails to compile and I get the following error message in my console:
console.error
Uncaught TypeError: Cannot read property 'settings' of undefined at Function.mapStateToProps [as mapToProps]
What am I doing wrong and how can I achieve my expected behavior?
I am storing my Firestore data as follows.
Firestore
.
├── users
| ├── OGk02kJbQUesTeVhTrLBnERSxrfm
| | ├── settings
| | | ├── VrxDnSxpUw6wgX0n9c1FbapmLaLa
| | | | ├── name: Waldo Garply
| | | | └── timestamp: 1546304499030
| | | ├── cGVHxSkU3Lcb9WAYWjnJKcLOTYf8
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | └── timestamp: 1546304499031
| | | ├── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | ├── mobile: 555-789-1234
| | | | └── timestamp: 1546304499032
Notice, I am storing a unique snapshot of all the settings values (including a timestamp) each time any of the settings values changes; I then fetch the latest setting (sorted by timestamp) and load it. So I am needing an automatic listener on the settings
object.
I am using the following code in my component (called DetailsTab.js) to try connect to Firestore to fetch the data then load it as a settings
variable into my component and Redux store.
DetailsTab.js
function mapStateToProps( state ) {
console.log('staten', state);
return {
user: state.auth.user,
// attempted all the following individually
settings: state.firestore.data.users.settings, // throws error
settings: state.firestore.ordered.users.settings, // throws error
settings: state.firestore.ordered.users[0] // throws error
settings: state.firestore.ordered.users // returns 'users' object
}
}
export default compose(
withStyles(styles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect(props => {
return [
{
collection: 'users',
doc: props.user.data.uid,
subcollections: [
{
collection: 'settings',
limit: 1,
orderBy: ['timestamp', 'desc',],
storeAs: 'settings',
},
],
},
];
})
)(DetailsTab)
The following is how the data appears when my console logs it.
console.log
state
└── firestore
├── data
| └── users
| └── OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings
| └── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
├── ordered
| └── users [Array(1)]
| └── 0
| ├── id: OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings [Array(1)]
| └── 0
| ├── id: qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
reactjs react-redux react-redux-firebase
I want to fetch a document called settings
from a Firestore subcollection, then load it into my local component (and Redux store) as a variable named settings
with the following value:
settings: {
name: 'Waldo Garply',
email: 'corge@foogle.net',
mobile: '555-789-1234',
timestamp: '1546304499032',
}
Instead, my app fails to compile and I get the following error message in my console:
console.error
Uncaught TypeError: Cannot read property 'settings' of undefined at Function.mapStateToProps [as mapToProps]
What am I doing wrong and how can I achieve my expected behavior?
I am storing my Firestore data as follows.
Firestore
.
├── users
| ├── OGk02kJbQUesTeVhTrLBnERSxrfm
| | ├── settings
| | | ├── VrxDnSxpUw6wgX0n9c1FbapmLaLa
| | | | ├── name: Waldo Garply
| | | | └── timestamp: 1546304499030
| | | ├── cGVHxSkU3Lcb9WAYWjnJKcLOTYf8
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | └── timestamp: 1546304499031
| | | ├── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| | | | ├── name: Waldo Garply
| | | | ├── email: corge@foogle.net
| | | | ├── mobile: 555-789-1234
| | | | └── timestamp: 1546304499032
Notice, I am storing a unique snapshot of all the settings values (including a timestamp) each time any of the settings values changes; I then fetch the latest setting (sorted by timestamp) and load it. So I am needing an automatic listener on the settings
object.
I am using the following code in my component (called DetailsTab.js) to try connect to Firestore to fetch the data then load it as a settings
variable into my component and Redux store.
DetailsTab.js
function mapStateToProps( state ) {
console.log('staten', state);
return {
user: state.auth.user,
// attempted all the following individually
settings: state.firestore.data.users.settings, // throws error
settings: state.firestore.ordered.users.settings, // throws error
settings: state.firestore.ordered.users[0] // throws error
settings: state.firestore.ordered.users // returns 'users' object
}
}
export default compose(
withStyles(styles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect(props => {
return [
{
collection: 'users',
doc: props.user.data.uid,
subcollections: [
{
collection: 'settings',
limit: 1,
orderBy: ['timestamp', 'desc',],
storeAs: 'settings',
},
],
},
];
})
)(DetailsTab)
The following is how the data appears when my console logs it.
console.log
state
└── firestore
├── data
| └── users
| └── OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings
| └── qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
├── ordered
| └── users [Array(1)]
| └── 0
| ├── id: OGk02kJbQUesTeVhTrLBnERSxrfm
| └── settings [Array(1)]
| └── 0
| ├── id: qoDYG2xloEvUUhGQyF9zXy9MTMIq
| ├── name: Waldo Garply
| ├── email: corge@foogle.net
| ├── mobile: 555-789-1234
| └── timestamp: 1546304499032
reactjs react-redux react-redux-firebase
reactjs react-redux react-redux-firebase
edited Jan 3 at 12:35
Mowzer
asked Jan 1 at 2:30


MowzerMowzer
5,548741107
5,548741107
InmapStateToProps
try to fetch the data bystate.firestore.ordered.users.settings
to see if the data you are looking for is there.
– Dez
Jan 1 at 22:13
@Dez: That did not work. I edited the question to show all the possible paths tosettings
I have attempted undermapStateToProps
.
– Mowzer
Jan 1 at 23:36
add a comment |
InmapStateToProps
try to fetch the data bystate.firestore.ordered.users.settings
to see if the data you are looking for is there.
– Dez
Jan 1 at 22:13
@Dez: That did not work. I edited the question to show all the possible paths tosettings
I have attempted undermapStateToProps
.
– Mowzer
Jan 1 at 23:36
In
mapStateToProps
try to fetch the data by state.firestore.ordered.users.settings
to see if the data you are looking for is there.– Dez
Jan 1 at 22:13
In
mapStateToProps
try to fetch the data by state.firestore.ordered.users.settings
to see if the data you are looking for is there.– Dez
Jan 1 at 22:13
@Dez: That did not work. I edited the question to show all the possible paths to
settings
I have attempted under mapStateToProps
.– Mowzer
Jan 1 at 23:36
@Dez: That did not work. I edited the question to show all the possible paths to
settings
I have attempted under mapStateToProps
.– Mowzer
Jan 1 at 23:36
add a comment |
1 Answer
1
active
oldest
votes
The solution was to add the appropriate error guards as follows.
function mapStateToProps( state ) {
console.log('staten', state);
const settings = state.firestore.ordered.users
&& state.firestore.ordered.users[0]
&& state.firestore.ordered.users[0].settings
&& state.firestore.ordered.users[0].settings[0];
return {
user: state.auth.user,
settings,
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992693%2fhow-to-retrieve-and-load-data-from-a-firestore-subcollection-using-react-redux-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution was to add the appropriate error guards as follows.
function mapStateToProps( state ) {
console.log('staten', state);
const settings = state.firestore.ordered.users
&& state.firestore.ordered.users[0]
&& state.firestore.ordered.users[0].settings
&& state.firestore.ordered.users[0].settings[0];
return {
user: state.auth.user,
settings,
}
}
add a comment |
The solution was to add the appropriate error guards as follows.
function mapStateToProps( state ) {
console.log('staten', state);
const settings = state.firestore.ordered.users
&& state.firestore.ordered.users[0]
&& state.firestore.ordered.users[0].settings
&& state.firestore.ordered.users[0].settings[0];
return {
user: state.auth.user,
settings,
}
}
add a comment |
The solution was to add the appropriate error guards as follows.
function mapStateToProps( state ) {
console.log('staten', state);
const settings = state.firestore.ordered.users
&& state.firestore.ordered.users[0]
&& state.firestore.ordered.users[0].settings
&& state.firestore.ordered.users[0].settings[0];
return {
user: state.auth.user,
settings,
}
}
The solution was to add the appropriate error guards as follows.
function mapStateToProps( state ) {
console.log('staten', state);
const settings = state.firestore.ordered.users
&& state.firestore.ordered.users[0]
&& state.firestore.ordered.users[0].settings
&& state.firestore.ordered.users[0].settings[0];
return {
user: state.auth.user,
settings,
}
}
answered Jan 7 at 9:26


MowzerMowzer
5,548741107
5,548741107
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992693%2fhow-to-retrieve-and-load-data-from-a-firestore-subcollection-using-react-redux-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In
mapStateToProps
try to fetch the data bystate.firestore.ordered.users.settings
to see if the data you are looking for is there.– Dez
Jan 1 at 22:13
@Dez: That did not work. I edited the question to show all the possible paths to
settings
I have attempted undermapStateToProps
.– Mowzer
Jan 1 at 23:36