Assign Badges sending a notification on every page visit; help!

@amandam sadly… as it turns out. My code only partially works.

It’s giving a member a badge when they click on a button, but it’s not adhering to the criteria/query the badge is supposed to have. Here’s my front end:

import { currentMember } from ‘wix-members’ ;
import { getMemberBadges , getTenuredBadge , getPatronBadge } from ‘backend/getBadges’
import wixData from ‘wix-data’

const options = {
fieldsets : [ ‘FULL’ ]
}
$w . onReady ( async function () {

$w ( '#myBadges' ). data  = [] 

**const**  loggedInMember  =  **await**  currentMember . getMember ( options ); 

**const**  memberBadges  =  **await**  getMemberBadges ( loggedInMember . _id ); 

**if**  ( memberBadges ) { 
    myBadges ( memberBadges ) 
} 

$w ( '#claimBadges' ). onClick ( **async**  () => { 

    **const**  TenuredBadge  =  **await** 
    wixData . query ( "Members/Badges" ) 
        . contains ( "title" ,  "Tenured" ) 
        . find (); 
    **const**  TenuredBadgeId  =  TenuredBadge . items [ 0 ]. _id ; 
    wixData . query ( "Forum/Posts" ) 
        . gt ( "likeCount" ,  0 ) 
        . find () 
        . then (( results ) => { 
            **if**  ( results . items . length  >  0 ) { 
                **const**  topPosts  =  results . items ; 
                **const**  member  =  topPosts . map ( post  =>  post . _memberId ); 
                getTenuredBadge ( TenuredBadgeId , [ member ]); 
            }  **else**  { 
                console . log ( "No top posts" ); 
            } 
        }) 
        . **catch** (( error ) => { 
            console . log ( error ); 
            "console.error();" 
        }); 

    **if**  ( TenuredBadge ) { 
        updateBadgeRepeater ( loggedInMember . _id ) 
        $w ( '#myBadges' ). show () 
        $w ( '#myBadges' ). expand () 

        **let**  PatronBadge  =  **await**  getPatronBadge ( loggedInMember . _id ); 

        **if**  ( PatronBadge ) { 
            updateBadgeRepeater ( loggedInMember . _id ) 
            $w ( '#myBadges' ). show () 
            $w ( '#myBadges' ). expand () 
        } 
    } 

}); 

**function**  myBadges ( memberBadges ) { 
    wixData . query ( "Members/Badges" ) 
        . hasSome ( '_id' ,  memberBadges [ 0 ]. badgeIds ) 
        . find () 
        . then (( results ) => { 
            console . log ( results . items ) 
            $w ( '#myBadges' ). data  =  results . items ; 
            $w ( '#myBadges' ). onItemReady (( $item ,  itemData ,  index ) => { 
                $item ( "#badgeName" ). text  =  itemData . title ; 
                $item ( "#badgeRules" ). text  =  itemData . description ; 
                $item ( "#badgeIcon" ). src  =  itemData . icon ; 
            }) 
            $w ( '#myBadges' ). expand (); 
        }); 
} 

**function**  updateBadgeRepeater ( memberId ) { 
    getMemberBadges ( memberId ). then (( results ) => { 
        **if**  ( results . length  >  0 ) { 
            myBadges ( results ) 
        } 
    }) 

} 

})


And the backend:

import { badges } from ‘wix-members-backend’ ;

export function getMemberBadges ( memberID ) {

return badges . listMemberBadges ([ memberID ])
. then (( memberBadges ) => {
return memberBadges ;
})
. catch (( error ) => {
console . error ( error );
});
}

export function getTenuredBadge ( loggedInMember ) {
const badgeId = “2e93fa8d-a13f-4f2a-b47c-cbc16b903581” ;

return badges . assignMembers ( badgeId , [ loggedInMember ])
. then (( assignedMembers ) => {
return assignedMembers ;
})
. catch (( error ) => {
console . error ( error );
});
}

export function getPatronBadge ( member ) {
const badgeId = “2a36d9ae-a8de-409d-aae5-3007cb9e81a5” ;

return badges . assignMembers ( badgeId , [ member ])
. then (( assignedMembers ) => {
return assignedMembers ;
})
. catch (( error ) => {
console . error ( error );
});
}


Thanks again for your time on this. This is the last piece of the puzzle before we launch the site!