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

ImageInput causes CastError: Cast to ObjectId failed for value "[]" at path "avatar" #24

Closed
twoolworth opened this issue May 25, 2020 · 3 comments

Comments

@twoolworth
Copy link

I am working on a small example application to get familiar with Strapi and React-Admin and am attempting to upload an image using the ImageInput component. I feel like I've read your documentation at https://github.com/nazirov91/ra-strapi-rest#file-upload and React-Admins at ImageInput at https://marmelab.com/react-admin/Inputs.html.

Right now I only have a single field that allows file upload so my App.js has:

const uploadFields = ["avatar"];

And is being passed in verbatim as what you've written in the documentation:

const dataProvider = simpleRestProvider('http://localhost:1337', httpClient, uploadFields);

For my create screen I also have your code verbatim as:

<ImageInput source="avatar" label="Avatar" accept="image/*">
     <ImageField source="url" title="name" />
</ImageInput>

Inside of my person.settings.json the field is referenced as:

"avatar": {
  "model": "file",
  "via": "related",
  "allowedTypes": [
	"images"
  ],
  "plugin": "upload",
  "required": false
},

Whenever I hit save on the form I see the following error logged from the server:

error CastError: Cast to ObjectId failed for value "[]" at path "avatar"
at new CastError (server\node_modules\mongoose\lib\error\cast.js:39:11)
at ObjectId.cast (server\node_modules\mongoose\lib\schema\objectid.js:246:11)

When I inspect the post request I do see it sending an empty array as the field which makes me think that's likely where the error is coming from, the POST request is:

-----------------------------3577480103102910508985816256
Content-Disposition: form-data; name="data"

{"fullname":"Bob Smith","avatar":[]}
-----------------------------3577480103102910508985816256--

I think this is an issue with how the uploaded files are being mapped to the fields but first wanted to see if you saw anything I had missed. I also wanted to thank you for providing the ra-strapi-rest as it did save me a ton of time in getting an example application up and running with React-Admin

@nazirov91
Copy link
Owner

Hi @twoolworth, thanks for reporting this issue.

Is the upload functionality working for multiple images?

@nazirov91
Copy link
Owner

Closing for no response

@wvanooijen92
Copy link

@nazirov91 I am running into this problem as well, it goes away when I change this to

        if(existingFileIds.length === 1){
            data[fieldName] = existingFileIds[0]; 
        }else if (existingFileIds.length > 1) {
            data[fieldName] = [...existingFileIds];
        }

I am not sure that this will play nice in all scenarios tho, maybe you have more insight on this.

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

3 participants