#[SOLVED] unable to user Appwrite Functions.

17 messages · Page 1 of 1 (latest)

pliant portal
#

these are the logs that the function gives on execution

#

i cant even find any docs to write the code related to appwrite database on the function

pure raptor
#
  1. Query is not defined, add an import for it.
  2. something doesn't seem correct with your document you are using in database.createDocument in moveExpiredTickets.
  3. isListedForSale should be a string, as per the error. Did you create the attribute type as a String on console dashboard?
pliant portal
#

ok i have resloved two errors, now only one is left

#
    at Databases.createDocument (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:1695:19)
    at moveExpiredTickets (file:///usr/local/server/src/function/src/main.js:148:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Module.default (file:///usr/local/server/src/function/src/main.js:170:5)
    at async execute (/usr/local/server/src/server.js:208:16)
    at async action (/usr/local/server/src/server.js:225:7)
    at async /usr/local/server/src/server.js:14:5```
#
  try {
    const ticketsCollectionId = process.env.TICKETS_COLLECTION_ID;
    const expiredTicketsCollectionId = process.env.EXPIRED_TICKETS_COLLECTION_ID;
    const databaseId = process.env.DATABASE_ID;

    if (!ticketsCollectionId || !expiredTicketsCollectionId || !databaseId) {
      throw new Error('Missing collection IDs or database ID in environment variables');
    }

    const tickets = await database.listDocuments(databaseId, ticketsCollectionId);

    for (let ticket of tickets.documents) {
      const eventDateStr = ticket.eventDate;
      const eventDate = new Date(eventDateStr);
      const currentDate = new Date();

      console.log('Processing ticket:', ticket);

      if (eventDate < currentDate) {
        

        console.log('Creating document with data:', ticket);

        await database.createDocument(
          databaseId, 
          expiredTicketsCollectionId, 
          ticket
        );
        await database.deleteDocument(databaseId, ticketsCollectionId, ticket.$id);

        console.log(`Moved ticket with ID: ${ticket.$id} to expired tickets collection.`);
      }
    }
  } catch (error) {
    console.error('Error moving expired tickets:', error);
  }
}```
pliant portal
pliant portal
#

@pure raptor help

mint arrow
# pliant portal ```async function moveExpiredTickets() { try { const ticketsCollectionId =...

As mention in your support ticket, it looks like the error you're getting is:

Missing required parameter: "data"

when creating a document. Your code is:

        await database.createDocument(
          databaseId, 
          expiredTicketsCollectionId, 
          ticket
        );

As mentioned in the docs, the 3rd parameter is expected to be the documentId and the 4th parameter is expected to be the data, but it looks like you're passing the data for the ID and nothing for the data. Perhaps you should be passing ID.unique() for the documentId param?

pliant portal
#

okay i'll try it

#
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:206:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.createDocument (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:1711:16)
    at async moveExpiredTickets (file:///usr/local/server/src/function/src/main.js:148:9)
    at async Module.default (file:///usr/local/server/src/function/src/main.js:171:5)
    at async execute (/usr/local/server/src/server.js:208:16)
    at async action (/usr/local/server/src/server.js:225:7)
    at async /usr/local/server/src/server.js:14:5```
#

this is the new error that it gives

#
        await database.createDocument(
          databaseId, 
          expiredTicketsCollectionId, 
          ID.unique(),
          ticket
        );```
#

this is the code that i updated

pure raptor
#

your ticket contains $id variable which isn't required/allowed here, hence the error. This will be probably caused by other params too [$permissions, $createdAt, $updatedAt] I guess. Better to extract the data that you want and only add that to the new document.
Refer - https://appwrite.io/docs/references/cloud/models/document

pliant portal
#

okay thanks, it solved, error's gone