Skip to content

Unable to create database document #20

Closed

Description

@doricci

First of all, thank you for your awesome work!!

I currently tring to create a set of service for Angular that will communicate with the REST API of Appwrite.

When I'm trying to create a new document in a collection from the database, I got a PHP error:

Warning: Illegal string offset '$permissions' in /usr/share/nginx/html/app/controllers/database.php on line 388

Notice: Array to string conversion in /usr/share/nginx/html/app/controllers/database.php on line 388

Fatal error: Uncaught TypeError: Argument 1 passed to Database\Database::createDocument() must be of the type array, string given, 
called in /usr/share/nginx/html/app/controllers/database.php on line 430 and defined in /usr/share/nginx/html/src/Database/Database.php:184 Stack trace: 
#0 /usr/share/nginx/html/app/controllers/database.php(430): Database\Database->createDocument('A"test":"test"}') 
#1 [internal function]: {closure}('5d7e5234e6d9d', 'A"test":"test"}', Array, Array, '', '', 'assign') 
#2 /usr/share/nginx/html/vendor/utopia-php/framework/src/App.php(401): call_user_func_array(Object(Closure), Array) 
#3 /usr/share/nginx/html/app/app.php(792): Utopia\App->run(Object(Utopia\Request), Object(Utopia\Response)) 
#4 /usr/share/nginx/html/public/index.php(27): include('/usr/share/ngin...') 
#5 {main} thrown in /usr/share/nginx/html/src/Database/Database.php on line 18

This error appear if I'm using a REST client or if I call the backend from my service.

This is my request :

  • Method: POST
  • path: /v1/database/{collectionId}/documents
  • headers :
{
  "content-type": "application/json",
  "x-appwrite-key":"API_KEY",
  "x-appwrite-project":"PROJECT_ID"
 }
  • body:
{
  "data":"{\"test\":\"test\"}",
  "read":[],
  "write":[],
  "parentDocument":"",
  "parentProperty":"",
  "parentPropertyType":"assign"
}

Is it me that is missing something from the documentation about the structure of the data to send or is this really a bug?

Activity

  1. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member

    @doricci, first of all seems like it is a bug, because the server is printing an uncaught error - I will make sure to fix it by the next release.

    Regarding this specific issue, you should pass the data param as a JSON object and not a string.

    Make sure you have created a collection with matching structure to be able to pass the document creation validation.

    If you have problems creating a new collection, we have a previous issue open about some parts missing in the docs. In the meantime we have a collection structure example here: #19

  2. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member

    I might also be able to check if data parameter is a string and try to parse as a JSON to try an avoid this kind of an obvious confusion.

  3. doricci commented on Sep 15, 2019

    @doricci
    Author

    @eldadfux thx for the quick answer!

    I'm sending the data as a json object

    {
      "data": {"test":"test"},
      "read": [],
      "write": [],
      "parentDocument": "",
      "parentProperty": "",
      "parentPropertyType": "assign"
    }

    and I get the current error now:

    {
       "message":"Bad structure. Invalid document (#): Missing collection attribute $collection",
       "code":400,
       "file":"\/usr\/share\/nginx\/html\/app\/controllers\/database.php",
       "line":434,
       "trace":[
          {
             "function":"{closure}",
             "args":[
                "5d7e5234e6d9d",
                {
                   "test":"test",
                   "$permissions":{
                      "read":[
                         "*"
                      ],
                      "write":[
                         "*"
                      ]
                   }
                },
                [
                   "*"
                ],
                [
                   "*"
                ],
                "",
                "",
                "assign"
             ]
          },
          {
             "file":"\/usr\/share\/nginx\/html\/vendor\/utopia-php\/framework\/src\/App.php",
             "line":401,
             "function":"call_user_func_array",
             "args":[
                {
    
                },
                {
                   "collectionId":"5d7e5234e6d9d",
                   "data":{
                      "test":"test"
                   },
                   "read":[
                      "*"
                   ],
                   "write":[
                      "*"
                   ],
                   "parentDocument":"",
                   "parentProperty":"",
                   "parentPropertyType":"assign"
                }
             ]
          },
          {
             "file":"\/usr\/share\/nginx\/html\/app\/app.php",
             "line":792,
             "function":"run",
             "class":"Utopia\\App",
             "type":"->",
             "args":[
                {
    
                },
                {
    
                }
             ]
          },
          {
             "file":"\/usr\/share\/nginx\/html\/public\/index.php",
             "line":27,
             "args":[
                "\/usr\/share\/nginx\/html\/app\/app.php"
             ],
             "function":"include"
          }
       ],
       "version":"latest"
    }
  4. doricci commented on Sep 15, 2019

    @doricci
    Author

    For all the call about the collection, I have no issues, I find that the REST Client and the JS SDK are clear and doesn't need a lot of knowledge to be understood

  5. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member

    Umm, this is another thing that should be fixed by next version.

    To fix it now just add '$collection' key to your data parameter JSON:

    {
      "$collection": "YOUR_COLLECTION_ID_SAME_AS_IN_PATH",
      "data": {"test":"test"},
      "read": [],
      "write": [],
      "parentDocument": "",
      "parentProperty": "",
      "parentPropertyType": "assign"
    }
  6. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member
  7. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member

    Committed a fix for the missing collection issue:
    a5c3e97

  8. doricci commented on Sep 15, 2019

    @doricci
    Author

    Thank you, now I'm able to add a document to the collection with the fix that you suggested, but the $collection has to be in the data object that is send.

    I waiting the next release to remove this fix from my code!

    I missed the documentation about the creation of the collection that you mentionned in #19 .

    But thank you again for the quick answer and the great work!!

  9. eldadfux commented on Sep 15, 2019

    @eldadfux
    Member

    Happy to help, I am already working on the fix, should be a one liner quick fix and probably be shipped in next version (might be out this week!).

  10. doricci commented on Sep 24, 2019

    @doricci
    Author

    Hey, thanks for your time, now I'm able to login and register a new user without the api key, my issue was with angular, the session cookie wasn't set after the login ....

    But now that I'm logged, the user is unable to create or read new collections.
    [email protected] (role: member) missing scope (collections.read) and I have [email protected] (role: member) missing scope (collections.write) when I try to create a new collections.
    The read and write of the same collection work when I set an API key.

    Did I miss something in the documentation about how to create a collection? Or do I need to update the current user to add some permission?

  11. eldadfux commented on Sep 24, 2019

    @eldadfux
    Member

    @doricci As mentioned before, this is a section of the documentation that is lacking and I am working on improving it + providing some real world examples.

    Currently, Appwrite only allows creation of new collection using a Server SDK + API key + Relevant Scope ('collections.write'). This is meant to avoid users from abusing your collection creation endpoint.

    If you want to allow all users to create new documents in a collection you created using a server SDK just create it with a public ("*") read permission.

  12. eldadfux commented on Sep 25, 2019

    @eldadfux
    Member

    @doricci another thing I forgot to mention, in coming version we will add support for creating collections directly from the dashboard, which will make this process easier when getting started.

  13. doricci commented on Sep 25, 2019

    @doricci
    Author

    Ok thanks!
    I'll wait the next release then.

  14. added 3 commits that reference this issue on Sep 21, 2026
  15. added a commit that references this issue on Sep 24, 2026
  16. added a commit that references this issue on Sep 24, 2026
  17. added a commit that references this issue on Sep 24, 2026
  18. added a commit that references this issue on Sep 25, 2026
  19. added 6 commits that reference this issue on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions