[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
- Update User Error
```ts const { users, databases } = await createAdminClient(); const session = await getLoggedInUser(); const user = await users.get(session.$id); if (!use...
- Our Appwrite organization is suspended
Please give support regarding this , no app is working now , please solve my issue and give support , no one is replying in message section or email.
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...