[SOLVED] Log: "Missing required user data" after executing a PHP function
- 0
- Resolved
- Android
- Accounts
- Cloud
- Users
- Functions
Maybe there is a problem with the payload that I send. This is an example: {
"name":"myname", "email":"myname@gmail.com", "password":"appassword123"
}
It seems like no user id is being sent?
Try this one
use Appwrite\ID;
...
$users->create(ID::unique(), $email,null, $password, $name);
I tried it, also I sent the userid in the payload and yet it shows the same response.
And you get the same error?
Yes
Can you share your code at this point?
Yes
What is the exact error?
In Appwrite there are no errors
Only the response
Good, and what exactly is this one?
Missing required user data: name, email, password
Okay, then the error is from here
if (empty($name) || empty($email) || empty($password)) {
return $res->send('Missing required user data: name, email, password', 400);
}
Meaning you'll need to parse the data in a different way.
Yes
Try this
try {
$userData = \json_decode($req['payload'], true);
} catch(\Exception $err) {
$res->json([
'success' => false,
'message' => 'Payload is invalid.',
]);
return;
}
Now you're $userData should have the parsed JSON.
Should I replace this with your code: try { $user = $users->create(ID::unique(), $email, null, $password, $name); $userId = $user['$id'] ?? ''; // Retrieve the generated user ID } catch (AppwriteException $error) { echo $error->getMessage(); return; }
This part you can leave the same
Here's the complete example
<?php
use Appwrite\Client;
use Appwrite\Services\Users;
use Appwrite\Services\Functions;
use Appwrite\ID;
require_once 'vendor/autoload.php';
return function ($req, $res) {
$client = new Client();
$users = new Users($client);
if (
!$req['variables']['APPWRITE_FUNCTION_ENDPOINT']
|| !$req['variables']['APPWRITE_FUNCTION_API_KEY']
|| !$req['variables']['APPWRITE_FUNCTION_PROJECT_ID']
) {
return $res->send('Environment variables are not set. Function cannot use Appwrite SDK.', 500);
}
$client
->setEndpoint($req['variables']['APPWRITE_FUNCTION_ENDPOINT'])
->setProject($req['variables']['APPWRITE_FUNCTION_PROJECT_ID'])
->setKey($req['variables']['APPWRITE_FUNCTION_API_KEY'])
->setSelfSigned(true);
// Debug code: Log the received payload
error_log('Received payload:');
error_log(print_r($req, true));
// Correctly access the user data from the payload
try {
$userData = \json_decode($req['payload'], true);
} catch(\Exception $err) {
$res->json([
'success' => false,
'message' => 'Payload is invalid.',
]);
return;
}
// Retrieve the user data fields from the payload
$userId = $userData['userid'] ?? '';
$name = $userData['name'] ?? '';
$email = $userData['email'] ?? '';
$password = $userData['password'] ?? '';
// Check for missing user data fields
if (empty($name) || empty($email) || empty($password)) {
return $res->send('Missing required user data: name, email, password', 400);
}
try {
$user = $users->create(ID::unique(), $email, null, $password, $name);
$userId = $user['$id'] ?? ''; // Retrieve the generated user ID
} catch (AppwriteException $error) {
echo $error->getMessage();
return;
}
$user = [
'userid' => ID::unique(),
'name' => $name,
'email' => $email,
'password' => $password
];
return $res->json(['user' => $user]);
};
Ok, thanks
It's solved now. Thanks
<a:agooglethumbsup:635256484682530825>
[SOLVED] Log: "Missing required user data" after executing a PHP function
Recommended threads
- Appwrite project paused
I have github student account. The auth has been paused however the database is working fine. I cant see any option to resume from my console panel. It is just ...
- MongoDb Database Breaks integer[] & bigi...
I've got a table with the below column created. When I try to insert the value `[-3408048000]` into it, the dart sdk cloud function call is successful (no error...
- Missing Invoice
Hi! I need help with billing on my account (marelu@gmail.com). I have never received a single invoice by email. My organization has been on the Pro plan since...