Uploading image and referencing users-permissions_user

System Information
  • Strapi Version: 3.5.4
  • Operating System: Windows WSL
  • Database: MongoDB
  • Node Version: 14.15.3
  • NPM Version: 6.14.9
  • Yarn Version: 1.19.1

Hello, I want to upload a file and also reference users-permissions.User collection to fill in the avatar field. This is my reference Can't successfully upload a file to an existing entry from Node JS - #9 by periabyte

Here is what I have right now:

const attachment = await strapi.plugins.upload.services.upload.upload({
  data: {
    refId: "607baacc2402bd32b213f639",
    ref: "users-permissions_user",
    field: "avatar",
  files: {
    path: "/home/jamesvibar/171116.gif",
    name: "171116.gif",
    type: "image/gif",

What ref should I use if I want to reference the users-permission.User model?

Here’s the error I receive

Error Log
backend: TypeError: Cannot read property 'associations' of undefined
backend:     at /home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-connector-mongoose/lib/relations.js:261:43
backend:     at Array.forEach (<anonymous>)
backend:     at /home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-connector-mongoose/lib/relations.js:242:20
backend:     at Array.reduce (<anonymous>)
backend:     at Function.update [as updateRelations] (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-connector-mongoose/lib/relations.js:97:68)
backend:     at processTicksAndRejections (internal/process/task_queues.js:93:5)
backend:     at runNextTicks (internal/process/task_queues.js:62:3)
backend:     at processImmediate (internal/timers.js:434:9)
backend:     at async Object.create (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-connector-mongoose/lib/queries.js:64:14)
backend:     at async Object.create (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-database/lib/queries/helpers.js:15:18)
backend:     at async Object.add (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-plugin-upload/services/Upload.js:302:17)
backend:     at async Promise.all (index 0)
backend:     at async Object.upload (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-plugin-upload/services/Upload.js:140:12)
backend:     at async Object.afterFindOne (/home/jamesvibar/dev/bdobarterbuddy/packages/backend/extensions/users-permissions/models/User.js:35:28)
backend:     at async executeLifecycle (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-database/lib/utils/lifecycles.js:7:5)
backend:     at async Object.findOne (/home/jamesvibar/dev/bdobarterbuddy/node_modules/strapi-database/lib/queries/helpers.js:18:3)
1 Like

Add plugin’s name as source field and model name inside the ref:

  data: {
    refId: "607baacc2402bd32b213f639",
    ref: "user",
    source: "users-permissions",
    field: "avatar",

Also, when you get this error: Cannot read property 'associations' of undefined on file uploads, that means the model that you indicated in ref is not correct.

1 Like

Thank you so much sir, I re-read the docs and the source field parameter was mentioned and I missed it.

Saved my ass!

Help! Trying to use the upload service as mentioned in this thread but my uploaded file (I am trying to add an avatar to users-permissions as well) never gets associated with the referenced user record. In debugging the service upload method, I can’t find anywhere where the ‘body’ data gets used to update the entity.


Solved. In Strapi V4 using REST:

  'ref': 'plugin::users-permissions.user',
  'refId': 116,
  'field': 'avatar',

it does’t work for me in Strapi v4.0.7

What doesn’t work exactly? If you are trying to add a file to an existing user entity, then these instructions work: Upload file to existing entity.

For a users-permissions user the fields are:
“ref”: “plugin::users-permissions.user”
“refId”: <the id of the user record you want to attach the file to, as a string>
“field”: <the name of your field in the user collection, for example ‘avatar’>

Make a multipart POST request to the url: http://localhost:1337/api/upload with your bearer token.

Make sure that you have allowed the upload action in the Authenticated Role under the Upload plugin section.

If you are trying to upload a file during a user registration (something that I needed), then it won’t work as it does with other content-types using the “Upload file during entry creation” instructions. Instead you have to re-implement the register function and upload the file after the user record is added (copy a lot of code from the plugin-users-permissions plugin auth controller) into your strapi-server.js file. If you do that, be warned as the dev team has updated the register code recently and it broke my version of it until I updated it to match their code.

I hope this helps.


I am facing an issue, I am uploading an image to “plugin::users-permissions.user”, the field is “profile_picture” using the multipart request, my problem is, if the image already exists in the field, then this method does not update the field with the new value, can anyone help me with this.

I’m having the same issue.

thanks, it worked for me. but it only works for upload (POST) user profile and didn’t work for update (PUT). how can i update user profile?