# Help correcting code

**URL:** <https://forum.wixstudio.com/t/help-correcting-code/788>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [April 25, 2022, 12:15pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788 "2022-04-25T12:15:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug-antill](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doug-antill](https://forum.wixstudio.com/u/doug-antill)\
**Post date:** [April 25, 2022, 12:15pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/1 "2022-04-25T12:15:08Z")

</div>

I’m trying to retrieve an event from the event collection and then use the \_id to retrieve the associated registration form but I’m running into problems with the getform.

**return** searchEventBackend ( slug )  
. then ( ( result ) =\> {  
$w ( ‘#eventTitle’ ). text = result . title ;  
**return** ( result );  
})  
. then ( eventResult =\> {  
eventData = eventResult ;  
eventId = eventData . \_id ;  
**return** wixEvents . getForm ( eventId )  
})  
. then ( formResult =\> {  
console . log ( formResult );

If I use return wixEvents.getForm I get Error: Request failed 401. If, on the other hand I remove the return then formResult is undefined.  
It can’t be that difficult can it? 😀

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [April 25, 2022, 4:40pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/2 "2022-04-25T16:40:37Z")

</div>

Please add more details:  
backend/front end?  
Show the function declaration.  
What are the collection permissions?  
Do you run it as Admin/member/visitor?  
etc…

---

<div class="post-metadata">

**Author:** ![doug-antill](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doug-antill](https://forum.wixstudio.com/u/doug-antill)\
**Post date:** [April 25, 2022, 7:36pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/3 "2022-04-25T19:36:58Z")

</div>

The code snippet is backend. searchEventBackend(slug) is a backend function to retrieve the event. WixEvents.getform(eventId) is the function to retrieve the form object.

```javascript
function populateEventFields() {
return searchEventBackend( slug )
  .then( (result) => {
    $w('#eventTitle').text=result.title;
    return(result);
  })
  .then (eventResult => {
    eventData=eventResult;
    eventId = eventData._id;
    return wixEvents.getForm(eventId)
})
  .then (formResult => {
    console.log(formResult);

```

I’m running it in preview and live as admin but it doesn’t work in either.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [April 25, 2022, 8:43pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/4 "2022-04-25T20:43:53Z")

</div>

You have an extra then(). Get rid of these lines (I don’t think it’ll resolve the issue, but getting rid of unnecessary code lines is good):

```javascript
 return(result);
  })
  .then (eventResult => {
  eventData=eventResult;

```

and keep using result instead of eventResult.:

```javascript
function populateEventFields() {
return searchEventBackend( slug )
  .then(result => {
  $w('#eventTitle').text = result.title;
    console.log(result) //WHAT DOES IT LOG? POST It HERE
    return wixEvents.getForm(result._id)
})
.then(form => {
    console.log(form.formData);
})
}

```

---

<div class="post-metadata">

**Author:** ![doug-antill](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doug-antill](https://forum.wixstudio.com/u/doug-antill)\
**Post date:** [April 26, 2022, 10:04am UTC](https://forum.wixstudio.com/t/help-correcting-code/788/5 "2022-04-26T10:04:52Z")

</div>

Thanks for that. I’ve tidied the code as you recommended but I still get Error: Request failed 401. The console.log(result) correctly displays the data from the backend search.

---

<div class="post-metadata">

**Author:** ![doug-antill](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doug-antill](https://forum.wixstudio.com/u/doug-antill)\
**Post date:** [April 26, 2022, 11:39am UTC](https://forum.wixstudio.com/t/help-correcting-code/788/6 "2022-04-26T11:39:14Z")

</div>

The only reference to an error 401 refers to not having permission to perform this action but I’m running in Preview mode and as admin. There’s no option in getForm for options so how do I ensure everyone has permission to get the form?

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [April 26, 2022, 12:24pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/7 "2022-04-26T12:24:13Z")

</div>

And doesn’t it work on live sites as well?  
Anyway, I’ve never worked with Wix Events, so I can’t tell.  
Maybe someone else will be able to assist.

---

<div class="post-metadata">

**Author:** ![doug-antill](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doug-antill](https://forum.wixstudio.com/u/doug-antill)\
**Post date:** [April 26, 2022, 3:59pm UTC](https://forum.wixstudio.com/t/help-correcting-code/788/8 "2022-04-26T15:59:57Z")

</div>

@jonatandor35 Thanks J.D. It used to work on both live and preview. I got a phone call to say it wasn’t working so I tried it in preview which gives this error. Weird or what?  
Anyway thanks for you help
