Back

[SOLVED] Appwrite sdk isn't working in the cloud function

  • 0
  • Databases
  • Functions
  • Web
manazo
7 Mar, 2023, 05:44

I think the client isn't initializing. I've added appwrite endpoint, project_id, and api_key. the project_id automatically comes from the request variable, endpoint & api_key are two that I've manually added from my appwrite console.

endpoint => https://localhost/v1

api_key => that I got from the API tab

I noticed the doc says that I need to use my public ip instead of https://localhost/v1 in case of cloud functions. but tbh, I don't really know what they mean by "public ip". we can find the same page on 127.0.0.1 as well. Do they want me to use https://127.0.0.1/v1 instead? if yes, then I'm not logged in there. and I also forgot my password ( i don't think we have any way to recover passwords in development mode )

I've added the code where I'm trying to access my DB and also the log of my function execution.

That's all I have. I'm not sure what I'm doing wrong. I'm new to Appwrite. So if anyone can help, that'll be appreciated.

NOTE: I'm on a Node env

TL;DR
The user initially faced an issue with the Appwrite SDK not working in a cloud function. Another user helped them realize that they missed adding `/v1/` to the endpoint. The issue was resolved and the SDK started working as expected. The user also asked for help regarding creating a document inside the Appwrite function. They received an error when trying to create a document, as well as when trying to retrieve a document from the Appwrite dashboard. Unfortunately, no solution was provided in the thread for this issue. Another user asked about recovering their Appwrite console password while in development mode. They had forgotten their password and needed to perform tasks
Meldiron
7 Mar, 2023, 09:44

Hey there πŸ‘‹ Your code looks fine. Hmm what seems to be hapenning here is your cod egetting to catch where you log error. For some reason, error object appears to have no public properties.

You can try to do console.log(error.toString() instead, it might have more information for you. Also add some dummy console.log("Here") into catch, to ensure that's where code is getting.

Meldiron
7 Mar, 2023, 09:44

I can't see why it would fail, to be honest. Seeing error will be really useful

manazo
7 Mar, 2023, 11:02

@Meldiron Hey. thanks for the toString() suggestion. i finally see some different message. As shown in the attached, it throws a type error saying client.setEndpoint(...).setProject(...).setKey is not a function, which is really unexpected. Cause the function-starter node project in GitHub also uses the same approach

manazo
7 Mar, 2023, 11:10

i just tried removing the setKey from my code and seems now the Client is initialized. but we face another error, saying "connect ECONNREFUSED 127.0.0.1:80". no idea what that mean

Meldiron
7 Mar, 2023, 11:59

Ooh you are using appwrite In function (in Node.JS), please use library node-appwrite. It's adjusted a bit with server-to-server features, such as API keys or Users API

Meldiron
7 Mar, 2023, 12:00

Cool. Yeah we need to provide proper IP address. With real server, that wuold be IP address of your server. With local development, you can LAN IP address of your device. You can usually find that in network settings. It's different for everyone

Meldiron
7 Mar, 2023, 12:00

In future we plan to make this simpler by having internal endpoint like appwrite, but for now, you need to provide LAN IP

manazo
7 Mar, 2023, 14:47

thanks for the help. I've replaced appwrite with node-appwrite, changed the EndPoint to my machine's IP address, and added the setKey again. now I see a different error message. it's maybe saying that I don't have read permission for that collection. I guess it's happening cause I'm not logged in from the public IP. I'm only logged in from http://localhost. And as I mentioned before, I forgot my console password. is there any walkaround for this? or any way to recover my pass?

Drake
7 Mar, 2023, 17:27

What scopes did you give your api key?

manazo
7 Mar, 2023, 18:58

i'm dumb

manazo
7 Mar, 2023, 18:58

it's solved. thanks

Drake
7 Mar, 2023, 18:58

[SOLVED] Appwrite sdk isn't working in the cloud function

manazo
8 Mar, 2023, 06:27

@Steven hey. the previous problem is solved. thanks for helping with that. I was wondering if there's any way to recover my appwrite console password while being in development mode. if forgot my pass, so now I need to do many of my tasks manually

Drake
8 Mar, 2023, 06:39

Best to create a new post

ashew
9 Mar, 2023, 19:53

Hey @manazo can you share how you solved this?

ashew
9 Mar, 2023, 20:00

@Steven I'm facing an issue for a similar use case. I'm creating a document inside the Appwrite function.

I've set the correct endpoint, project ID, API key with necessary scopes.

I get this error when I try to createDocument

TypeScript
Error: Not Found
    at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Databases.createDocument (/usr/code-start/node_modules/node-appwrite/lib/services/databases.js:1055:16)
    at async module.exports (/usr/code-start/index.js:52:24)
    at async /usr/local/src/server.js:68:13```

And when I create a document from Appwrite dashboard and I try to `getDocument` I get this error 

Error: app.63d65be8dbea5fcd8b44@service.192.168.29.173 (role: applications) missing scope (home) at Client.call (/usr/code-start/node_modules/node-appwrite/lib/client.js:171:31) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Databases.getDocument (/usr/code-start/node_modules/node-appwrite/lib/services/databases.js:1088:16) at async module.exports (/usr/code-start/index.js:61:24) at async /usr/local/src/server.js:68:13```

Drake
9 Mar, 2023, 20:21

did you include /v1 in your endpoint?

ashew
9 Mar, 2023, 20:28

I missed adding /v1/ to the endpoint πŸ€¦β€β™‚οΈ. Apologies, that was a silly mistake. Thanks @Steven , it's working as expected now.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more