Missing display of JSON data in Appwrite function log
P粉302160436
P粉302160436 2023-09-15 00:25:51
0
1
601

I created a function using php to create a new user in Appwrite. After execution, the following is the output:

  1. Response: Required user data is missing.
  2. Log: No log recorded
  3. Error: No error logged
<?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('环境变量未设置。函数无法使用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('缺少必需的用户数据。', 400);
    }

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

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

P粉302160436
P粉302160436

reply all(1)
P粉463418483

All your user data may be empty because you are reading from $req['data'] but the input data is in $req['payload'] .

The document description is as follows:

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template