How to fix error when reading 'getCurrentMember' in filtering Webflow collections by logged-in member? Answered

Post author
Paul Mosig

Hi, I am looking to implement Julian Galluzzo 's tutorial on how to filter a collection based on the current logged in member (this one: https://filtering-deleting-collection-items.webflow.io/)

I am getting the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'getCurrentMember')

The page definitely knows who the current logged in member is as I am using other member data successfully on the page. Here is a loom describing the issue in more detail.

What is weird is if I paste in the JS directly into the console it works - but the same code before the </body> tag does not.

Thanks!

Comments

1 comment

  • Comment author
    Marc Hudson

    Hmm, I can't see your code in Loom but you can try this to see if it works for you. It can be simplified somewhat. I'm just sharing what I use from various files:

    const checkKeyExists = function(obj, keys) {
            // If obj is falsy.
            if (!(!!obj)) return false;
            keys = typeof keys === 'string' ? keys.split('.') : keys;
            if (keys.length === 0) return true;
            return checkKeyExists(obj[ keys.shift() ], keys);
        };

    const waitFor = function(key, value, timer, callback) {
            var nTimer = setInterval(function() {
                // wait for something to load...
                if (checkKeyExists(key, value)) {
                    callback();
                    clearInterval(nTimer);
                }
            }, timer);
        };

    const getCurrentMember = function(callback) {
            waitFor(window, "$memberstackDom", 50, function() {
                window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
                    member = member || {};

                    if (!!callback) callback(member);
                    return member;
                });
            });
        };

    // usage:
    getCurrentMember(function(member) {
        // Do something with "member"...
    });

    0

Please sign in to leave a comment.