How to fix error when accessing Memberstack's getCurrentMember method in JavaScript? Answered
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
Hey @Marko Jurisic, can you try testing it with this code instead and see if it works?
"Download timestamp added"
It actually worked, what was the problem ?
THANK YOU MY MAN
You were using window.memberstack instead of window.$memberstackDom. AJ just declared a variable to reference to the correct library 😉
Welcome 😇
Please sign in to leave a comment.