0

I created a function with php to create a new user in Appwrite. After the execution these are the outputs:

  1. Response: Missing required user data.
  2. Logs: No logs recorded
  3. Errors: No errors recorded
<?php

use Appwrite\Client;
use Appwrite\Services\Users;

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

    $userData = $req['data'] ?? [];

    $userId = $userData['userid'] ?? '';
    $name = $userData['name'] ?? '';
    $email = $userData['email'] ?? '';
    $password = $userData['password'] ?? '';

    if (empty($userId) || empty($name) || empty($email) || empty($password)) {
        return $res->send('Missing required user data.', 400);
    }

    $user = [
        'userid' => $userId,
        'name' => $name,
        'email' => $email,
        'password' => $password
    ];

    return $res->json(['user' => $user]);
};

harlindis
  • 1
  • 1

1 Answers1

0

All your user data is probably empty because you're reading from $req['data'], but the input data is in $req['payload'].

The docs state:

When your function is called, you receive two parameters, a request and a response object. The request object contains all data that was sent to the function including function variables. A schema of the request object can be found below and is the same for all runtimes.

Property Description
headers An object containing all the request headers.
payload A JSON string containing the data when you created the execution.
variables An object containing all the function variables. This includes variables automatically added by Appwrite.

For more information, see https://appwrite.io/docs/functions#writingYourOwnFunction.

Steven Nguyen
  • 452
  • 4
  • 4