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 global variables
when i create a top-level global variable in go or bun how will it behave? is the heap getting renewed on every execution or could i do some kind of temp. cachi...
- Appwrite Auth & Function don't reveal cl...
When I execute a function or sign in with my Flutter app, Appwrite does not show my real IP: instead, it seems that Appwrite shows Fastly CDN IP address.
- Why does this happen?
`AppwriteException: general_argument_invalid, Invalid `secret` param: Value must be a valid string and at least 1 chars and no longer than 256 chars (400)` the...