Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create database document #20

Closed
doricci opened this issue Sep 15, 2019 · 13 comments
Closed

Unable to create database document #20

doricci opened this issue Sep 15, 2019 · 13 comments

Comments

@doricci
Copy link

doricci commented Sep 15, 2019

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?

@eldadfux
Copy link
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

@eldadfux
Copy link
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.

@doricci
Copy link
Author

doricci commented Sep 15, 2019

@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"
}

@doricci
Copy link
Author

doricci commented Sep 15, 2019

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

@eldadfux
Copy link
Member

eldadfux commented Sep 15, 2019

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"
}

@eldadfux
Copy link
Member

@doricci feel free to join us on gitter as well.

https://gitter.im/appwrite/community

@eldadfux
Copy link
Member

Committed a fix for the missing collection issue:
a5c3e97

@doricci
Copy link
Author

doricci commented Sep 15, 2019

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!!

@eldadfux
Copy link
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!).

@doricci
Copy link
Author

doricci commented Sep 24, 2019

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?

@eldadfux
Copy link
Member

eldadfux commented Sep 24, 2019

@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.

@eldadfux
Copy link
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.

@doricci
Copy link
Author

doricci commented Sep 25, 2019

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

@doricci doricci closed this as completed Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants