#[SOLVED] Problems with adding a new document in appwrite via function

62 messages · Page 1 of 1 (latest)

astral musk
#

It looks like your title is inside eventData like so

 if (empty($payload['eventData']['title'])) {
            return $res->json([
                'success' => false,
                'message' => 'Title is required',
            ], 400);
        }

You can avoid that by doing so

 $payload = json_decode($req['payload'], true);
 $payload = $payload['eventData'];

And then continue everything else as usual.

boreal heart
#

I changed the code to this and it keeps showing the error that title is required

boreal heart
#

Warning: Trying to access array offset on value of type null in /usr/code-start/src/index.php on line 35

astral musk
#

Can you var_dump the payload.

boreal heart
#

Response: {"success":false,"message":"Invalid payload. eventData is missing."}

astral musk
#

Can you log this, the payload

try{
        $payload = json_decode($req['payload'], true);
        var_dump($payload);

And share it

boreal heart
#

Yes. the reponse: {"success":false,"message":"Invalid payload. eventData is missing."} the logs: NULL

astral musk
#

And how do you call the function?

boreal heart
#

with this payload: {
"eventData": {
"eventId": ID::unique(),
"title": "New Event",
"description": "Event description",
"owner": "648051d777d7ac754e75",
"image": "64b4fec93805d8f21470",
"location": "Event location",
"date": "18/07/2023 11:57:45.251",
"endDate": "19/07/2023 11:58:17.701",
"innocaps": ["tag1", "tag2", "tag3"]
}
}

astral musk
#

But where?

boreal heart
#

in appwrite by clicking execute button

#

cloud

astral musk
#

Ohh,
I don't think you can write ID::unique() in the popup

#

Try this instead

{
  "eventData": {
    "eventId": "unique()",
    "title": "New Event",
    "description": "Event description",
    "owner": "648051d777d7ac754e75",
    "image": "64b4fec93805d8f21470",
    "location": "Event location",
    "date": "18/07/2023 11:57:45.251",
    "endDate": "19/07/2023 11:58:17.701",
    "innocaps": ["tag1", "tag2", "tag3"]
  }
}
boreal heart
#

No i think the correct form is using the ID because I just tried this and it faield

#

failed

astral musk
#

But ID::unique is a function
You try to like so ☝️?

boreal heart
#

I treid it before and it worked

astral musk
#

Interesting, don't think it should

astral musk
boreal heart
#

Yes, it failes

astral musk
#

What is your code right now
And what you're getting now?

boreal heart
#

{"success":false,"message":"Invalid payload. eventData is missing."} logs:NULL

boreal heart
astral musk
#

I don't see this error in your code Invalid payload. eventData is missing when is it being printed?

boreal heart
astral musk
boreal heart
astral musk
# boreal heart

You can't use the ID::unique here as it will make the json invalid
it has to be something like

 "eventId": "unique()",

But also I see that you don't use it in your code so you can remove that line and try again

boreal heart
#

I deleted it in the code but i send the event id in the payload and i get the same error

#

maybe there is a issue with decoding the paylaod

astral musk
#

Not in the code

#

Leave it in the code

astral musk
astral musk
boreal heart
#

Maybe i should not create an array EventData with all these elements because it is more complicated

astral musk
#

This is not the problem
That is just fine

astral musk
boreal heart
#

yes it failed with no response showing

astral musk
#

Mmm
We can try one more thing
Can you share the payload data?

boreal heart
#

async
:
true
data
:
"{\n "eventData": {\n "eventId": ID::unique(),\n "title": "New Event",\n "description": "Event description",\n "owner": "648051d777d7ac754e75",\n "image": "64b4fec93805d8f21470",\n "location": "Event location",\n "date": "18/07/2023 11:57:45.251",\n "endDate": "19/07/2023 11:58:17.701",\n "innocaps": ["tag1", "tag2", "tag3"]\n }\n

astral musk
#

You send the ID::unique() it won't work with it
Can you replace the whole string with this and try

{
  "eventData": {
    "eventId": "unique()",
    "title": "New Event",
    "description": "Event description",
    "owner": "648051d777d7ac754e75",
    "image": "64b4fec93805d8f21470",
    "location": "Event location",
    "date": "18/07/2023 11:57:45.251",
    "endDate": "19/07/2023 11:58:17.701",
    "innocaps": ["tag1", "tag2", "tag3"]
  }
}
boreal heart
#

{"data":"{\n "eventData": {\n "eventId": "unique()",\n "title": "New Event",\n "description": "Event description",\n "owner": "648051d777d7ac754e75",\n "image": "64b4fec93805d8f21470",\n "location": "Event location",\n "date": "18/07/2023 11:57:45.251",\n "endDate": "19/07/2023 11:58:17.701",\n "innocaps": ["tag1", "tag2", "tag3"]\n }\n}","async":true}

astral musk
#

Good
And you got on this one?

boreal heart
#

the execution with the payload that you sent was failed

wind burrow
boreal heart
#

Response: {"success":false,"message":"Invalid payload. eventData is missing."} Logs: NULL

wind burrow
boreal heart
#

Response: {"success":false,"message":"Invalid payload. eventData is missing."} Logs: string(360) "{
"eventData": {
"eventId": ID::unique(),
"title": "Sample Event",
"description": "This is a sample event description.",
"owner": "648051d777d7ac754e75",
"image": "64b4fec93805d8f21470",
"location": "Sample Location",
"date": "20/07/2023 11:57:45.251",
"endDate": "21/07/2023 11:57:45.251",
"innocaps": ["innocap"]
}
}
"

#

No, there are no errors recorded

wind burrow
boreal heart
#

No, this doesn't solve the problem for me

wind burrow
boreal heart
#

Response: 'Invalid payload. eventData is missing. Logs: string(341) "{
"eventId": "ID::unique()",
"title": "Sample Event",
"description": "This is a sample event description.",
"owner": "648051d777d7ac754e75",
"image": "64b4fec93805d8f21470",
"location": "Sample Location",
"date": "24/07/2023 11:57:45.251",
"endDate": "25/07/2023 11:57:45.251",
"innocaps": ["innocap"]
}
"

wind burrow
scenic egret
#

I solved it by deleting eventData and only using payload. It works now