
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
- Import "appwrite.exceptions" could not b...
Import "appwrite.exceptions" could not be resolved I tried using many versions of appwrite but this error still occuring. Please help me to fix it.
- [Self Hosted]
fresh instance of Appwrite - cant deploy function from local to instance ā Error ⢠func2 (68134cd9002358f96e4a) ⢠Invalid `specification` p...
- dart function very slow
sometimes waiting too long, about 3mins to 5mins, sometimes very fast, not build time, just execute, anyway to speed up?
