Thank you so much ! This information will serve me as sure soon, but unfortunately it does not answer my question ^^
The problem is that I don't execute anything on the client side, the function is automatically triggered with an event (like creating a user)
In this case, how can I retrieve the user information that was created?
Ahhhh okay! In that case, you can use Realtime.
Realtime ?
Alright one sec
You can read detailed information about Realtime here: https://appwrite.io/docs/realtime
But in short, it's a way for you to subscribe to changes being made to anything in your Appwrite project
There's a lot of channels that you can subscribe to, which can be found here: https://appwrite.io/docs/realtime#channels
But we agree that I don't need to run anything with any SDK?
Realtime works best with the SDK that appwrite supplies
Have a look at the Realtime docs. It should help you
For example, if your function is triggered by a new document creation, you can use the new document's ID (which is returned after the document is created) to subscribe to any changes being made to that document
I hope that makes sense
I think I understood something. I'll try, but I don't think it's exactly the same thing I want to do. I'll be back in about ten minutes to try something then I'll let you know ^^
const { Client, Storage, Query, Permission } = require("node-appwrite")
module.exports = async function (req, res) {
const client = new Client()
const storage = new Storage(client)
client.setEndpoint(req.variables.APPWRITE_FUNCTION_ENDPOINT)
.setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.variables.APPWRITE_FUNCTION_API_KEY)
const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
const { userId } = JSON.parse(user)
const bucketId = `user-${userId}`
const list = await storage.listBuckets([Query.equal("$id", [bucketId])])
if (list.total) {
res.send(`Bucket ${bucketId} already exists`, 409)
return
}
const bucket = await storage.createBucket(bucketId, bucketId, [
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.delete(Role.user(userId))
], true)
res.json(bucket, 201)
}
This is the kind of functions that will be triggered by the creation event of a user. For the moment I have a connection refused error when running, but the idea is there ^^
Look at the event data variable. See https://appwrite.io/docs/functions#functionVariables
Thank you ^^ Indeed I was able to retrieve the data, however I don't understand why I can't create a bucket :/ Here is my error and my code:
const { Client, Storage, Role, Permission } = require("node-appwrite")
module.exports = async function (req, res) {
const client = new Client()
const storage = new Storage(client)
client.setEndpoint("https://back.transferra.cc/v1")
.setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.variables.APPWRITE_FUNCTION_API_KEY)
const user = req.variables.APPWRITE_FUNCTION_EVENT_DATA
const { userId } = JSON.parse(user)
const bucketId = `user-${userId}`
const bucket = await storage.createBucket(bucketId, bucketId, [
Permission.read(Role.user(userId)),
Permission.write(Role.user(userId)),
Permission.delete(Role.user(userId))
], true)
res.json(bucket, 201)
}
Error: User (role: guests) missing scope (buckets.write)
at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Storage.createBucket (/usr/code-start/node_modules/node-appwrite/lib/services/storage.js:115:16)
at async module.exports (/usr/code-start/src/index.js:27:20)
at async /usr/local/src/server.js:68:13
I use an API key with rights to create buckets.
User guest means API key wasn't set 🧐 you created a variable and put in the API key? Maybe you can update your code to log the Appwrite function API key variable
Indeed error on my part, I had well defined the variable but the content of the key was wrong 🤣 I have two questions and then I think it's all good for me.
When I deploy a function via Appwrite CLI, the triggers get deleted every time and I have to recreate them from the appwrite interface. How to prevent this?
My second question: Is TypeScript supported?
You will need to put your variables and any other function settings in your appwrite.json.
For typescript, for now, you'll have to transpile before deploying and deploy the transpiled code.
My package.json has a build script: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/appwrite-functions/create-photo/package.json#L7
And then the entry point is the transpiled js file: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/appwrite.json.default#L438
Oh okay ! That's damn good lol A big thank you to you both steven and safwan ^^
The more I use Appwrite, the more I love it, the team too :'))
It's good ! Everything works great :)
[SOLVED] How functions work
Recommended threads
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...