How to retrieve and load data from a Firestore subcollection using React-Redux-Firebase?












1















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









share|improve this question

























  • 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
















1















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









share|improve this question

























  • 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














1












1








1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:35







Mowzer

















asked Jan 1 at 2:30









MowzerMowzer

5,548741107




5,548741107













  • 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















0














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,
}
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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,
    }
    }





    share|improve this answer




























      0














      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,
      }
      }





      share|improve this answer


























        0












        0








        0







        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,
        }
        }





        share|improve this answer













        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,
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 7 at 9:26









        MowzerMowzer

        5,548741107




        5,548741107
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter