GetEvent()

The “documentation” for getEvent is not helping me. getEvent - Velo API Reference - Wix.com
All I want to do is retrieve the event title when someone RSVPs. The onRsvpCreated module contains the eventId so that is easy enough, but how do I get the event title out of the results of the Promise.

I can’t believe that no one has ever done this and that there isn’t an example.

The getEvent() API (that you linked to above) has an example code snippet that shows how to call getEvent(). The snippet is code that should be in the backend. When calling it from the frontend, you need to handle the returned Promise, and then get the field that you want (title).

You should modify the code so that you can send it the id of the event you want. Put this code in a backend web-module (.jsw) file:

import { wixEvents } from "wix-events-backend";

export function myGetEventFunction(eventId) {
  return wixEvents.getEvent(eventId)
    .then((result) => {
      return result;
    })
    .catch((error) => {
      console.error(error);
    });
}

From the frontend you would call it something like this:

import {myGetEventFunction} from'backend/<name of your backend file>'; 

let myEvent = < id of the event you want to retrieve >
myGetEvent(myEvent).then(function(event) {
    console.log('event', event);
    let title = event.title; 
    console.log('event title', title);
});

See Velo Web Modules: Calling Backend Code from the Frontend for more information.

Also, to learn more about dealing with Promises, see the following:

  • Promises, Promises

  • Velo: Working with Promises

  • Video: Asynchronous Development and Velo

Thanks Yisrael,

That works, we’re just not quite there yet. I’ve got this chicken and egg thing going on, but I think we are close.

The second part of the script you provided I’ve put in the backend events.js file. I’m using the onRsvpCreated() module to send the guest list to Google Sheets as people respond. The eventId is provide by the onRsvpCreated() trigger, so I kind of need to nest the myGetEventFunction inside that so I can use the eventId but then I can’t get the “title” variable out to use in the rest of the function

import { appendValuesWrapper } from ‘backend/googlesheet-wrapper.jsw’ ;
import { myGetEventFunction } from ‘backend/getEvent.jsw’ ;

export function wixEvents_onRsvpCreated ( event ) {

**const**  eventId  =  event . eventId ; 

myGetEventFunction ( eventId ). then ( **function** ( event ) { 
console . log ( 'event' ,  event ); 
**let**  title  =  event . title ;  
console . log ( 'event title' ,  title ); 

});

**const**  rsvp  =  event . rsvpForm . inputValues ; 
//console.log(rsvp); 
**const**  bb  =  rsvp [ 0 ]. value ; 
**const**  formemail  =  rsvp [ 1 ]. value ; 
**const**  formlastName  =  rsvp [ 2 ]. value ; 
**const**  formfirstName  =  rsvp [ 3 ]. value ; 
**const**  addGuests  =  "0" 
console . log ( title ); 
 //let eventName = eventDetails.title; 

// console.log(eventName + “!!!”)
try {
let comment = rsvp [ 4 ]. value ;
let phone = rsvp [ 5 ]. value ;
const values =[ title , formfirstName , formlastName , formemail , phone , addGuests , bb , comment ];
console . log ( values );
const res = appendValuesWrapper ( values );
}

**catch** { 
  **let**  comment  =  "No Comment" 
  **let**  phone  =  rsvp [ 4 ]. value ; 
  **const**  values  =[ title ,  formfirstName ,  formlastName ,  formemail ,  phone ,  addGuests ,  bb ,  comment ]; 
  **const**  res  =  appendValuesWrapper ( values ); 
} 

}

I worked it out eventually. Once I declared the variable globally using “var” and not “let” it worked.

import { appendValuesWrapper } from ‘backend/googlesheet-wrapper.jsw’;

import {myGetEventFunction} from ‘backend/getEvent.jsw’;

var eventId;
var title;

export function wixEvents_onRsvpCreated(event) {

eventId = event.eventId

const name = myGetEventFunction(eventId).then(function(event){
title=event.title;
console.log(title)
});

and so on as per above.