Unable to upload a file - Cannot read property 'associations' of undefined

System Information
  • Strapi Version: 3.6.0

I am trying to upload a file to Strapi. I keep receiving the error `TypeError: Cannot read property ‘associations’ of undefined.

I am following a course and my code is code for code in the course so It may be due to strapi 3.6.0.

My frontend code:

 const handleSubmit = async e => {
    e.preventDefault();
    const formData = new FormData();
    formData.append('files', image);
    formData.append('ref', 'homes');
    formData.append('refId', houseId);
    formData.append('field', 'image');

    const res = await fetch(`${API_URL}/upload`, {
      method: 'POST',
      body: formData,
    });

You must set headers ‘multipart/form-data’ and image source.

This code works.

const updatePicture = async (picture, axios) => {
    const formData = new FormData()
    formData.append('files', picture.files)
    formData.append('ref', picture.ref) // optional, you need it if you want to link the image to an entry
    formData.append('refId', picture.refId) // optional, you need it if you want to link the image to an entry
    formData.append('field', picture.field)
    formData.append('source', picture.source)
    await axios.post('/upload', formData, {
        headers: {
            'content-type': 'multipart/form-data'
        }
    })
}

I tried your solution (with Axios) and it’s still giving me the same status 500 error. Strange!

Did you found a Solution?

@davidgbell answer given by @manutepowa is right. But it’s ok if the header is not set. The issue is probably because of the source name. The source value should be the plugin name in which your entry exists. For the tables that you create source name is “content-manager”. Giving the correct source name i.e., correct plugins should work. Also, check for the correct ref and field name as well.

1 Like

@vinay_sandesh i am having almost the same error

error TypeError: Cannot read property 'associations' of undefined
    at D:\websites\fineback\node_modules\strapi-connector-bookshelf\lib\relations.js:259:46
    at Array.forEach (<anonymous>)
    at D:\websites\fineback\node_modules\strapi-connector-bookshelf\lib\relations.js:253:16
    at Array.reduce (<anonymous>)
    at Function.update [as updateRelations] (D:\websites\fineback\node_modules\strapi-connector-bookshelf\lib\relations.js:105:68)
[2022-01-20T09:45:55.874Z] debug POST /upload (7228 ms) 500
here is my code 
const handleSubmit = async (e) => {
    e.preventDefault();
    const formData = new FormData();
    formData.append("files", image);
    formData.append("ref", "sports");
    formData.append("refId", sportNewsId);
    formData.append("field", "image");
    formData.append('source', 'content-manager')

    const res = await fetch(`${API_URL}/upload`, {
      method: "POST",
      body: formData,
    });
    if (res.ok) {
      imageUploaded();
      
    }
  };

what do i need to do
thanks

Anybody found a solution to this, I am also getting server 500 error?