# Change a button link according to a data collection

**URL:** <https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680>\
**Category:** Ask the community\
**Tags:** wix-members-area, question, code\
**Created:** [March 24, 2020, 11:59pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680 "2020-03-24T23:59:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbeneitezkumo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/nbeneitezkumo/32/17838_2.png) [@nbeneitezkumo](https://forum.wixstudio.com/u/nbeneitezkumo)\
**Post date:** [March 24, 2020, 11:59pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/1 "2020-03-24T23:59:47Z")

</div>

Hey there! I need a button to take each of my site members to a different link.  
I have a site member area, but the collection is read only, so I created another collection with the same email and a corresponding link. I simply want the link of a button to change according to which site member is logged in.  
But I can’t for the life of me get this to work. I tried doing this with a reference field, but it wouldn’t work, and then I tried this code:

```javascript
// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixUsers from 'wix-users';
import wixData from 'wix-data';

let user = wixUsers.currentUser;

$w.onReady(function () {
    user.getEmail()
    .then( (email) => {
        let userEmail = email;
        console.log(userEmail);
        wixData.query("Links").eq("email", userEmail).find()
        .then( (results) => {
            if(results.items.length === 1) {
                let userLink = results[0].link;
                console.log(userLink);
                $w("#linkButton").enable();
                $w("#linkButton").link = userLink;
            } else {
                $w("#linkButton").disable();
            }
     } )
     .catch( (error) => {
         let errorMsg = error.message;
         let code = error.code;
         } );
     } );
});

```

This works out well when I preview, but when I test it live the button is never enabled… 🙄

I’m very new to this, I know my code might not be very efficient so if anyone has a better idea of how to do this I would greatly appreciate your help! 😀

---

<div class="post-metadata">

**Author:** ![tony-brunsman](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tony-brunsman](https://forum.wixstudio.com/u/tony-brunsman)\
**Post date:** [March 25, 2020, 12:44am UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/2 "2020-03-25T00:44:17Z")

</div>

Natalia,

A couple things to consider.

I’m not sure how it’s working when you preview because the userLink assignment syntax isn’t quite right. The data that is returned is in the items array within results. So, this would be the link of the first item:

```javascript
let userLink = results.items[0].link;

```

After changing this, I’m wondering if you will still receive different results in preview as opposed to the live page. If so, I would check that the contents of the Links collection is exactly the same in the live version as it is in the sandbox?

---

<div class="post-metadata">

**Author:** ![nbeneitezkumo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/nbeneitezkumo/32/17838_2.png) [@nbeneitezkumo](https://forum.wixstudio.com/u/nbeneitezkumo)\
**Post date:** [March 25, 2020, 3:44pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/3 "2020-03-25T15:44:12Z")

</div>

Hello Anthony, thank you for your help!  
It appears that now my button is enabled but when I click it it doesn’t redirect me anywhere 😕 I thought it might be because I’m not telling wix what should happen when I click the button. So I added this piece:

```javascript
export function linkButton_click(event) {
    wixLocation.to($w("#linkButton").link);
}

```

It does nothing so I’m not sure what’s going on here…  
Do yo have any clue of where I might be missing something?Or maybe a better way to implement this?  
Thanks

---

<div class="post-metadata">

**Author:** ![tony-brunsman](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tony-brunsman](https://forum.wixstudio.com/u/tony-brunsman)\
**Post date:** [March 25, 2020, 4:51pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/4 "2020-03-25T16:51:22Z")

</div>

What does console.log(userLink) return in the developer console at the bottom?

You might verify that the linkButton onClick event is still “registered” in the property sheet as follows. If you don’t see “linkButton\_click”, then the code block above will do nothing.

![](https://us1.discourse-cdn.com/wix/original/3X/4/c/4c002467b5bf2ccf9128fb6d60aee9c12578b5ff.png)

---

<div class="post-metadata">

**Author:** ![nbeneitezkumo](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/nbeneitezkumo/32/17838_2.png) [@nbeneitezkumo](https://forum.wixstudio.com/u/nbeneitezkumo)\
**Post date:** [March 25, 2020, 7:58pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/5 "2020-03-25T19:58:37Z")

</div>

The console.log(userLink) returns the link I want to go to. I made sure that the onClick event is registered in the properties panel, but still when I go on live it doesn’t lead me to my link… 😥It doesn’t matter how much times I click nothing happens. I already made sure to specify the target of the button with

```javascript
$w("#linkButton").target = "_self";

```

But it doesn’t help either…

---

<div class="post-metadata">

**Author:** ![annaleise\_hall](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@annaleise\_hall](https://forum.wixstudio.com/u/annaleise_hall)\
**Post date:** [April 1, 2020, 4:12pm UTC](https://forum.wixstudio.com/t/change-a-button-link-according-to-a-data-collection/30680/6 "2020-04-01T16:12:01Z")

</div>

I’m trying to do the same thing, so if anyone has more input, that would be fantastic 🙂
