
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
- Appwrite functions can't connect to data...
I'm trying to create a function that queries my database, but all database operations timeout from within the function, even though CLI access works perfectly. ...
- Error
I'm trying to get sellerId using account.get() in my appwrite function and this is the error message I'm getting: "Failed to send notification to seller 6865bf...
- Is there a timeframe for updating dart r...
Dart 3.5 is already one year old, and the following versions have introduced very important features for the language. Is there a roadmap or timeframe for when...
