php 使用Appwrite函数在特定集合中创建文档的问题

svujldwt  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(42)

我在尝试使用PHP函数在Appwrite中创建新文档时遇到了一个问题。Appwrite函数应该接收包含文档创建所需数据的有效负载,包括“title”和“owner”字段。然而,当我将有效载荷发送给函数时,我得到了一个响应,指示“Missing required event data:标题、所有者”,即使我确定在有效载荷中包括这些字段。

<?php

use Appwrite\Services\Account;
use Appwrite\Services\Avatars;
use Appwrite\Services\Databases;
use Appwrite\Services\Functions;
use Appwrite\Services\Health;
use Appwrite\Services\Locale;
use Appwrite\Services\Storage;
use Appwrite\Services\Teams;
use Appwrite\Services\Users;
use Appwrite\Client;
use Appwrite\ID;


require_once 'vendor/autoload.php';

return function ($req, $res) {
  $client = new Client();
  $account = new Account($client);
  $avatars = new Avatars($client);
  $database = new Databases($client);
  $functions = new Functions($client);
  $health = new Health($client);
  $locale = new Locale($client);
  $storage = new Storage($client);
  $teams = new Teams($client);
  $users = new Users($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);

    error_log('Received payload:');
    error_log(print_r($req, true));

    try {
        var_dump($req['payload']);
        $payload = json_decode($req['payload'], true);
    } catch (\Exception $err) {
        $res->json([
            'success' => false,
            'message' => 'Payload is invalid.',
        ]);
        return;
    }

    $eventId = $payload['eventId'] ?? '';
    $title = $payload['title'] ?? '';
    $description = $payload['description'] ?? '';
    $owner = $payload['owner'] ?? '';
    $image = $payload['image'] ?? '';
    $location = $payload['location'] ?? '';
    $date = $payload['date'] ?? '';
    $endDate = $payload['endDate'] ?? '';
    $innocaps = $payload['innocaps'] ?? [];

    if (empty($title) || empty($owner)) {
        return $res->json([
            'success' => false,
            'message' => 'Missing required event data: title, owner',
        ], 400);
    }

    $databaseId = 'conni'; // Replace with your actual database ID
    $collectionId = 'events'; // Replace with your actual collection ID

   
       
    

    try {
        $response = $database->createDocument($databaseId, $collectionId, $eventId, $title, $description, $owner, $image, $location, $date, $endDate, $innocaps);
    } catch (AppwriteException $error) {
        echo $error->getMessage();
        return;
    }

    return $res->json([
        'success' => true,
        'message' => 'Event created successfully',
        'eventId' => $response,
    ]);
};

字符串

9vw9lbht

9vw9lbht1#

看起来你的JSON字符串格式不正确(“ID::unique()”部分),所以json_decode()返回false,所以当你执行$payload['title']时,你不能从中提取数据。有效的JSON应该是:

{
    "eventId": "unique()",
    "title": "New Event",
    "description": "Event description",
    "owner": "648051d777d7ac754e75",
    "image": "64b4fec93805d8f21470",
    "location": "Event location",
    "date": "20/07/2023 11:57:45.251",
    "endDate": "21/07/2023 11:58:17.201",
    "innocaps": [
        "tag1",
        "tag2",
        "tag3"
    ]
}

字符串

相关问题