nazirov91/ra-strapi-rest

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

Closed this issue · 3 comments

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

Hi @twoolworth, thanks for reporting this issue.

Is the upload functionality working for multiple images?

Closing for no response

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