I have implemented a PHP function in an Appwrite project with the goal of deleting a document. However, the function is not working as expected, and I need assistance in troubleshooting the issue. The function is designed to receive a document ID as input and use the Appwrite PHP SDK to delete the corresponding document from the specified collection. In the code below, after I send a payload with document ID and execute it, the response is: 'Missing required data: documentId'.
<?php
use Appwrite\Client;
use Appwrite\Services\Databases;
require_once 'vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', '1');
return function ($req, $res) {
$client = new Client();
$database = new Databases($client);
if (
empty($req['variables']['APPWRITE_FUNCTION_ENDPOINT']) ||
empty($req['variables']['APPWRITE_FUNCTION_API_KEY']) ||
empty($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);
try {
$data = json_decode($req['payload'], true);
} catch (\Exception $err) {
$res->json([
'success' => false,
'message' => 'Payload is invalid.',
]);
return;
}
$documentId = $data['documentId'] ?? '';
$databaseId = 'conni';
$collectionId = 'innocaps';
if (empty($documentId)) {
return $res->send('Missing required data: documentId', 400);
}
try {
$response = $database->deleteDocument($databaseId, $collectionId, [$documentId]);
} catch (AppwriteException $error) {
echo $error->getMessage();
return;
}
$response = [
'documentId' => $documentId
];
return $res->json(['user' => $user]);
};