Back

[SOLVED] Log: "Missing required user data" after executing a PHP function

  • 0
  • Android
  • Accounts
  • Cloud
  • Users
  • Functions
sidney
5 Jul, 2023, 14:10

Maybe there is a problem with the payload that I send. This is an example: {

"name":"myname", "email":"myname@gmail.com", "password":"appassword123"

}

TL;DR
[SOLVED] Log: "Missing required user data" after executing a PHP function The issue was solved. The error was caused by missing required user data: name, email, and password. The solution was to correctly parse the user data from the payload and ensure all the required fields are present. The code snippet provided demonstrates how to handle the payload and check for missing data.
Binyamin
5 Jul, 2023, 14:10

It seems like no user id is being sent?

Binyamin
5 Jul, 2023, 14:10

Try this one

TypeScript
use Appwrite\ID;
...
$users->create(ID::unique(), $email,null, $password, $name);
sidney
5 Jul, 2023, 14:11

I tried it, also I sent the userid in the payload and yet it shows the same response.

Binyamin
5 Jul, 2023, 14:13

And you get the same error?

sidney
5 Jul, 2023, 14:13

Yes

Binyamin
5 Jul, 2023, 14:13

Can you share your code at this point?

sidney
5 Jul, 2023, 14:13

Yes

sidney
5 Jul, 2023, 14:14
Binyamin
5 Jul, 2023, 14:16

What is the exact error?

sidney
5 Jul, 2023, 14:16

In Appwrite there are no errors

sidney
5 Jul, 2023, 14:16

Only the response

Binyamin
5 Jul, 2023, 14:17

Good, and what exactly is this one?

sidney
5 Jul, 2023, 14:17

Missing required user data: name, email, password

Binyamin
5 Jul, 2023, 14:18

Okay, then the error is from here

TypeScript
  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.

sidney
5 Jul, 2023, 14:18

Yes

Binyamin
5 Jul, 2023, 14:20

Try this

TypeScript
try {
 $userData = \json_decode($req['payload'], true);
} catch(\Exception $err) {
 $res->json([
  'success' => false,
  'message' => 'Payload is invalid.',
 ]);
 return;
}
Binyamin
5 Jul, 2023, 14:20

Now you're $userData should have the parsed JSON.

sidney
5 Jul, 2023, 14:23

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; }

Binyamin
5 Jul, 2023, 14:23

This part you can leave the same

Binyamin
5 Jul, 2023, 14:24

Here's the complete example

TypeScript
<?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]);
};
sidney
5 Jul, 2023, 14:24

Ok, thanks

sidney
5 Jul, 2023, 14:51

It's solved now. Thanks

Binyamin
5 Jul, 2023, 14:52

<a:agooglethumbsup:635256484682530825>

Guille
5 Jul, 2023, 15:09

[SOLVED] Log: "Missing required user data" after executing a PHP function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more