[SOLVED] Log: "Missing required user data" after executing a PHP function
- 0
- 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
- DeploymentStatus enum value `canceled` m...
Hey, Sorry if it has been reported already, I found an issue using the Dart SDK where the `canceled` enum value is missing from `DeploymentStatus`. This causes...
- Frequent 500 Internal Server Errors - Pr...
PROJECT ID: 6951f2xxxxx1ee9e750a REGION: Singapore (sgp.cloud.appwrite.io) PLAN: Pro Our production application experiences **500 Internal Server Errors** mult...
- Issue with Custom SMTP
Hey everyone, I'm facing an issue when updating my Custom SMTP settings on Appwrite Cloud. The UI fails silently (no visual errors), but when checking the cons...