Help correcting code

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? :grinning:

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…

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.

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.

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):

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

and keep using result instead of eventResult.:

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);
})
}

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.

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?

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.

@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