I have appwrite function where I want to create a user.
Payload I am sending via console custom data interface
"{\"email\": \"user@example.com\",\"password\": \"example\",\"name\": \"User example\"}"
My code
if (req.payload) {
const payload = JSON.parse(res.payload);
console.log('PAYLOAD PARSED');
console.log(payload);
try {
const promise = await users.create(node_appwrite_1.ID.unique(), payload.email, payload.password, payload.name);
response.body = promise;
response.statusCode = 200;
}
catch (error) {
response.body = error;
}
}
It fails on JSON.parse from what I can tell ...
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at module.exports (/usr/code-start/src/index.js:31:30)
at /usr/local/src/server.js:68:19
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
But if I would do this in a javascript file
console.log(JSON.parse("{\"email\": \"user@example.com\",\"password\": \"example\",\"name\": \"User example\"}"))
I would get this in the console
// [object Object]
{
"email": "user@example.com",
"password": "example",
"name": "User example"
}
Am I doing something wrong here? Isn't the payload format correct?
Hi @Matej ! Try this:
const payload = JSON.parse(req.payload ?? "{}");
I think is not res but req
omg yes
I didn't see that typo
π
Thanks π
[SOLVED] Appwrite Function - JSON.parse returns an error
You are welcome
Recommended threads
- Function executions fail with cURL error...
Appwrite version: 1.8.0 Executor version: 0.11.4 Function runtime: Python 3.12 SELF-HOSTED Function executions that occur at the same moment as the executor's ...
- Broken Appwrite canβt make functions nor...
Hii guys, I was having this issue with my locally hosted Appwrite, I canβt create functions ( both template and manual), I canβt make a custom domain ( like in ...
- Tips for Debugging Appwrite Functions Lo...
Hi everyone! π I have an Appwrite Function running locally with Docker, but Iβm struggling to debug it because execution doesnβt reach the breakpoints I set. ...