Issue With Cloud Function

I am attempting to call the cloud function called getAllEvents, however when I do, I get an error in the console saying “Uncaught (in promise) Error: result.get is not a function”. Slightly unsure as to why this is happening.

Moralis.Cloud.define('getAllEvents', async (req) => {
  const query = new Moralis.Query('Event');
  req.params.onlyUpcoming && query.greaterThan('eventDate', new Date());
  const result = await query.find();
  const results = {
    eventDescription: result.get('eventDescription'),
    eventName: result.get('eventName'),
    eventOrganiser: result.get('eventOrganiser'),
    eventVenue: result.get('eventVenue'),
    eventDate: new Intl.DateTimeFormat('en-GB', {
      dateStyle: 'short',
      timeStyle: 'short',
    }).format(result.get('eventDate')),
    tickets: await Moralis.Cloud.run('getTicketsForEvent', {
      eventId: result.get('objectId'),
      limit: undefined,
    }), // getTicketsForEvent(result.get('objectId')),
  };
  return results;
});

Moralis.Cloud.define('getTicketsForEvent', async (req) => {
  const query = new Moralis.Query('Ticket');
  query.equalTo('eventId', req.params.eventId);
  req.params.limit && query.limit(req.params.limit);
  const queryResult = await query.find();
  const results = queryResult.map((x) => ({
    objectId: x.get('id'), // this is returning undefined
    eventId: x.get('eventId'),
    numberMinted: x.get('numberMinted'),
    art: x.get('artURL'),
    color1: x.get('color1'),
    color2: x.get('color2'),
    fontColor: x.get('fontColor'),
    ticketName: x.get('ticketName'),
    ticketPrice: x.get('ticketPrice'),
    ticketQuantity: x.get('ticketQuantity'),
  }));
  return results;
});

You may have a list in that result, you can add some logging

The logs say

2021-11-15T14:58:57.629Z - TypeError: result.get is not a function
at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:107:21), :1:216)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:95:5)
2021-11-15T14:58:57.627Z - Failed running cloud function getAllEvents for user cCYLUBTgMUJW8NhSX3dHgH2D with:
Input: {“onlyUpcoming”:true}
Error: {“message”:“result.get is not a function”,“code”:141}

Yes, that could happen if result is not what you expect it to be there. You can use logger.info(JSON.stringify(result)) to see what you have there.

Hey @b7011343

The `results is an array

find returns an array. If you need only one result - use query.first() instead :raised_hands:

Ah yes I see the issue. I have now modified my code to account for this, but I am now receiving a new error.

Uncaught (in promise) Error: Invalid function: “getAllEvents”

Moralis.Cloud.define('getAllEvents', async (req) => {
  const query = new Moralis.Query('Event');
  req.params.onlyUpcoming && query.greaterThan('eventDate', new Date());
  const queryResult = await query.find();
  const results = queryResult.map(async (x) => ({
    eventDescription: x.get('eventDescription'),
    eventName: x.get('eventName'),
    eventOrganiser: x.get('eventOrganiser'),
    eventVenue: x.get('eventVenue'),
    eventDate: new Intl.DateTimeFormat('en-GB', {
      dateStyle: 'short',
      timeStyle: 'short',
    }).format(x.get('eventDate')),
    tickets: await Moralis.Cloud.run('getTicketsForEvent', {
      eventId: x.get('objectId'),
      limit: undefined,
    }),,
  }));
  return results;
});

Moralis.Cloud.define('getTicketsForEvent', async (req) => {
  const query = new Moralis.Query('Ticket');
  query.equalTo('eventId', req.params.eventId);
  req.params.limit && query.limit(req.params.limit);
  const queryResult = await query.find();
  const results = queryResult.map((x) => ({
    objectId: x.get('id'),
    eventId: x.get('eventId'),
    numberMinted: x.get('numberMinted'),
    art: x.get('artURL'),
    color1: x.get('color1'),
    color2: x.get('color2'),
    fontColor: x.get('fontColor'),
    ticketName: x.get('ticketName'),
    ticketPrice: x.get('ticketPrice'),
    ticketQuantity: x.get('ticketQuantity'),
  }));
  return results;
});