Appending data into an existing array in content type

This discussion has been migrated from our Github Discussion #7899


Hello

I am new to strapi and react.

I have a mongodb which stores strapi content types. One of the content types know as ‘jobs’ has an array field ‘applicants’

Whenever someone applies to this job the userId of the applicant should get appended into the above array field ‘applicants’

The data comes from the frontend via:

`values = {
      Positions : 3,
      applicants: [
      localStorage.getItem('userId') // pass user applicant id
    ]

    }
    axios.put(API_JOB_APPLY+'/'+jobId, values)
    .then((response) => {
      console.log('applied');
    }).
    catch((err) => {

    });`

However, this just replaces the existing value in the applicants field.

The mongodb document looks like this:

{
"_id":{"$oid":"5f5775c039e4d291c04c0e93"},
"Description":"<p>Looking for a highly motivated and talented systems developer to join our growing team.</p>",
"isActive":true,
"Title":"Sys Devs",
"Positions":{"$numberInt":"3"},
"Location":[
{
"_id":{"$oid":"5f5b2fbb464f9bb1f7176479"}
,"kind":"ComponentLocationLocation",
"ref":{"$oid":"5f5775c039e4d291c04c0e94"}
}
],
"createdAt":{"$date":{"$numberLong":"1599567296843"}},
"updatedAt":{"$date":{"$numberLong":"1600155320269"}},"__v":{"$numberInt":"1"},
"Category":{"$oid":"5ef9f380843bad4f96c0625d"},"Owner":{"$oid":"5e41c8898c8ec51768be7f99"},
"applicants":[
{"$oid":"5e166816fc759725e96a2d1c"}
]
}

Does something needs to be done into the Strapi API, which would take the applicant value from put and maybe push it into the array?

Any help would be much appreciated.

Responses to the discussion on Github


derrickmehaffy79d ago

Collaborator

What type of relation are you using between the jobs and applicants?


greneit79d ago

Author

Hello

Its a one to many relationship
i.e. Job has many applicants


derrickmehaffy79d ago

Collaborator

I’m wondering if your relation is setup backwards in that one applicant has many jobs? Is your project code somewhere public like on Github so I can take a look?


greneit79d ago

Author

ah not sure how the backward setup works but the repo is on github so I’ll add you as a contributor.


greneit79d ago

Author

ok I’ve added you as a collaborator


derrickmehaffy79d ago

Collaborator

Ah you are using a version of Strapi that isn’t supported anymore :confused: the Beta versions of Strapi have been depreciated


greneit78d ago

Author

Ah ok. thanks for looking into this.
How do I upgrade from 3.0.0-beta.18.4 to 3.1.4?
I don’t want to lose all the content types and the modifications done relating to the different roles and permission.


greneit78d ago

Author

Further to this, I’ve upgraded to 3.1.4 and pushed it on the github repo.

I followed this to do the upgrade:

However, when I run npm start on my local dev system I get the following message (I have removed the not null constraint for admin username)

robinedwards@pop-os:/media/robinedwards/data/Documents/Development/workzone-app/server$ npm start

workzone@1.0.1 start /media/robinedwards/data/Documents/Development/workzone-app/server
strapi start

[2020-09-16T12:08:41.810Z] debug Server wasn’t able to start properly.
[2020-09-16T12:08:41.812Z] error TypeError: Cannot convert undefined or null to object
at Function.values ()
at Object.load (/media/robinedwards/data/Documents/Development/workzone-app/server/node_modules/strapi-database/lib/connector-registry.js:17:39)
at DatabaseManager.initialize (/media/robinedwards/data/Documents/Development/workzone-app/server/node_modules/strapi-database/lib/database-manager.js:32:21)
at Strapi.load (/media/robinedwards/data/Documents/Development/workzone-app/server/node_modules/strapi/lib/Strapi.js:299:19)
at async Strapi.start (/media/robinedwards/data/Documents/Development/workzone-app/server/node_modules/strapi/lib/Strapi.js:158:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! workzone@1.0.1 start: strapi start
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the workzone@1.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/robinedwards/.npm/_logs/2020-09-16T12_08_41_824Z-debug.log

I’ve attached the log file too:
2020-09-16T12_08_41_824Z-debug.log


derrickmehaffy78d ago

Collaborator

There are some migration guides you need to follow: Strapi’s documentation | Strapi Documentation


greneit69d ago

Author

I just tried updating it from .18 beta to .19 beta
After npm install, I ran npm start and got the following error:
obinedwards@pop-os:/media/robinedwards/data/Documents/Development/workzone-app/server$ npm start

workzone@0.1.0 start /media/robinedwards/data/Documents/Development/workzone-app/server
strapi start

[2020-09-25T11:43:27.896Z] debug Server wasn’t able to start properly.
[2020-09-25T11:43:27.897Z] error Error creating endpoint get /getInfos: policy.split is not a function or its return value is not iterable
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! workzone@0.1.0 start: strapi start
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the workzone@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/robinedwards/.npm/_logs/2020-09-25T11_43_27_909Z-debug.log

Do you know which file is the policy.split function is and what needs to be updated?

I followed the instructions from here:


derrickmehaffy69d ago

Collaborator

It looks like you have a custom policy assigned to a custom route called getInfos


greneit65d ago

Author

Thanks. Where in the code will I be able to remove this policy and route? I looked everywhere but I don’t see any route with getInfos


derrickmehaffy65d ago

Collaborator

It will either be somewhere in ./api/*/config/routes.json or possibly a plugin. Is your project somewhere public that I can take a look?


greneit65d ago

Author

I have four custom models and I’ve looked at all their routes but dont see getInfos. I’ve pushed the latest changes to github, you are already added as a collaborator on the repo.


greneit65d ago

Author

https://github.com/greneit/workzone-server


derrickmehaffy65d ago

Collaborator

It would appear from your project you haven’t upgraded, and have some issues due to some of your dependencies:

  "dependencies": {
    "strapi": "3.0.0-beta.19",
    "strapi-admin": "3.0.0-beta.19",
    "strapi-connector-mongoose": "3.0.0-beta.19",
    "strapi-plugin-content-manager": "3.0.0-beta.19",
    "strapi-plugin-content-type-builder": "3.0.0-beta.19",
    "strapi-plugin-documentation": "^3.0.0-beta.19",
    "strapi-plugin-email": "3.0.0-beta.19",
    "strapi-plugin-graphql": "^3.0.0-beta.19",
    "strapi-plugin-upload": "3.0.0-beta.19",
    "strapi-plugin-users-permissions": "3.0.0-beta.19",
    "strapi-utils": "3.0.0-beta.19"
  },

Specifically
"strapi-plugin-documentation": "^3.0.0-beta.19", and "strapi-plugin-graphql": "^3.0.0-beta.19",

The getInfos route comes from the documentation plugin:

Hence the source of your issue.


derrickmehaffy65d ago

Collaborator

Those ^ icons are what is cause the failure, as it’s likely trying to install the latest v3.1.6 but the rest of your project is still on beta.19 (FYI the beta is no longer supported)


greneit64d ago

Author

Ok thanks. I will remove the ^


greneit64d ago

Author

I’ve removed and I’ve migrated it to v 3.0
Everything was working fine until beta 20. However, once I migrated it from beta 20 to stable 3, the dashboard page does not load anything. The server console is not showing any errors but the browser console has got some errors, which I paste below.
I’ve update the github repo with the latest changes. What would be causing this - I’ve tried it on different browsers but its the same. I can get to the admin login screen, and login but after that it goes to localhost:1337/dashboard and it all blank. Any help would be appreciated.

TypeError: "d.default[e.label] is undefined"
f http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
f http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Za http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
vc http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
sl http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
cl http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
$c http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Yo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
unstable_runWithPriority http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Bo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Yo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Go http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
el http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
notify http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
notifyNestedSubs http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
handleChangeWrapper http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
v http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
e http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
_invoke http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
b http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
n http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
c http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
main.59bc0076.chunk.js:1:3719398
ec http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
callback http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
fa http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
ac http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
pl http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
unstable_runWithPriority http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Bo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
fl http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
$c http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Yo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
unstable_runWithPriority http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Bo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Yo http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
Go http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
el http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
notify http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
notifyNestedSubs http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
handleChangeWrapper http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
v http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
e http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
s http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
_invoke http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
b http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
n http://localhost:1337/dashboard/main.59bc0076.chunk.js:1
c http://localhost:1337/dashboard/main.59bc0076.chunk.js:1

derrickmehaffy64d ago

Collaborator

Per the beta to stable migration guide here: Strapi’s documentation | Strapi Documentation

You need to refactor all your config files in the ./config folder. You are still using the older beta configuration structure (.json files and the environments folder)

See the following documentation, and I recommend you read through all of the configuration section so you can get an idea of how the new structure is laid out: Strapi’s documentation | Strapi Documentation


greneit57d ago

Author

Thanks for this. I finally managed to get to the latest stable version 3 of strapi.
However, I still have the problem of appending data to a model which has an array. This time I am trying to add onto the Employers model from Jobs
Every job has one employer and
every employer has many jobs.
I’ve attached the screenshot.

From the fontend, when a job is created, I create the following array to update employer in the mongodb.
var jobValue; jobValue ={ jobs: [ newjob._id ] }


axios.put(API_EMPLOYERS_ENDPOINT+‘/’+employer._id, jobValue)

This only overwrites the existing value in the employers.jobs array.

Is there any other solution which I can use to append to the existing job in the employers table?

Thanks
PS: I’ve updated the server repository on github, if you want to take a look


derrickmehaffy57d ago

Collaborator

Currently no we don’t support appending like that (as Strapi doesn’t understand if you are adding a new one, or simply overwriting/deleting the others)

You will need to include existing IDs of the array + any new ones.

(Side point you should use id and not _id)


greneit55d ago

Author

Thanks for this. I appreciate your time and effort in helping me resolve this.