
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
- Can't create a function. The user interf...
I am trying to create a server-side function. I am on the free tier. **I already have three functions that work properly** that I created a few months ago. Now,...
- Cors error in appwrite functions
I am facing cors error in appwrite functions ,intially it was working fine now showing cors error
- I have the error: "User with the request...
Hello, I'm using a function in Appwrite and the GET and POST method works well, the POST method creates a user with the method users.create() , but when I try t...
