How to fix error when accessing Memberstack's getCurrentMember method in JavaScript? Answered

Post author
Marko Jurisic

hey guys trying to run this code:

<script>
document.addEventListener('DOMContentLoaded', function() {
const downloadLinks = document.querySelectorAll('[data-download="true"]');
downloadLinks.forEach(link => {
link.addEventListener('click', async function() {
try {
// Check if a member is logged in
const member = await window.memberstack.getCurrentMember();
if (!member.data) {
console.log('User is not logged in');
return;
} // Get the current member's JSON data
const jsonData = await window.memberstack.getMemberJSON();
const currentJson = jsonData.data || {}; // Initialize downloads array if it doesn't exist
const downloads = currentJson.downloads || []; // Add the current timestamp
downloads.push(new Date().toISOString());
currentJson.downloads = downloads; // Update the member's JSON data
await window.memberstack.updateMemberJSON({ json: currentJson });
console.log('Download timestamp added');
} catch (error) {
console.error('Error updating downloads:', error);
}
});
});
});
</script>

it triggers an event after clicking on a button, but i keep getting this error in the console:

"Error updating downloads: TypeError: Cannot read properties of undefined (reading 'getCurrentMember')
    at HTMLAnchorElement. "

But im logged in as a member while testing

Comments

4 comments

  • Comment author
    A J

    Hey @Marko Jurisic, can you try testing it with this code instead and see if it works?

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const downloadLinks = document.querySelectorAll('[data-download="true"]');
        const memberstack = window.$memberstackDom;
        downloadLinks.forEach(link => {
            link.addEventListener('click', async function() {
                try {
                    // Check if a member is logged in
                    const member = await memberstack.getCurrentMember();
                    if (!member.data) {
                        console.log('User is not logged in');
                        return;
                    }
                    // Get the current member's JSON data
                    const jsonData = await memberstack.getMemberJSON();
                    const currentJson = jsonData.data || {};
                    // Initialize downloads array if it doesn't exist
                    const downloads = currentJson.downloads || [];
                    // Add the current timestamp
                    downloads.push(new Date().toISOString());
                    currentJson.downloads = downloads;
                    // Update the member's JSON data
                    await memberstack.updateMemberJSON({ json: currentJson });
                    console.log('Download timestamp added');
                } catch (error) {
                    console.error('Error updating downloads:', error);
                }
            });
        });
    });
    </script> 
    
    0
  • Comment author
    Marko Jurisic

    "Download timestamp added"

    It actually worked, what was the problem ?

    THANK YOU MY MAN

    0
  • Comment author
    Raquel Lopez

    You were using window.memberstack instead of window.$memberstackDom. AJ just declared a variable to reference to the correct library 😉

    0
  • Comment author
    A J

    Welcome 😇

    0

Please sign in to leave a comment.