#[SOLVED] unable to user Appwrite Functions.
17 messages · Page 1 of 1 (latest)
i cant even find any docs to write the code related to appwrite database on the function
Queryis not defined, add an import for it.- something doesn't seem correct with your document you are using in
database.createDocumentinmoveExpiredTickets. isListedForSaleshould be a string, as per the error. Did you create the attribute type as aStringon console dashboard?
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);
}
}```
the error is post mrobably occuring in the createDocument part
@pure raptor help
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?
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
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
okay thanks, it solved, error's gone