I’m trying to move some functionality into a plugin and while the cronjobs work, I’m unable to get my content type to show up.
I’m following Add a content-type to a plugin - How to create a Strapi v4 plugin and https://www.youtube.com/watch?v=kUfYValVQVA&list=PL7Q0DQYATmvjd5D57P8CN0_xp_HsRd3wn&index=4
I have
'use strict';
const schema = require('./schema');
module.exports = {
schema,
};```
```// ./src/plugins/notifications/server/content-types/task/task.json
{
"kind": "collectionType",
"collectionName": "tasks",
"info": {
"singularName": "task",
"pluralName": "tasks",
"displayName": "Task"
},
"options": {
"draftAndPublish": false,
"comment": ""
},
"attributes": {
"name": {
"type": "string",
"required": true,
"maxLength": 40
},
"isDone": {
"type": "boolean",
"default": false
}
}
}```
and ```//./src/plugins/notifications/server/content-types/index.js
'use strict';
const task = require('./task');
module.exports = {
task
};```
I have also run yarn build from /plugins and then yarn develop. At this point I should be able to see the Task content-type in my CMS right?
<i>This topic has been created from a Discord post (1278376975811219547) to give it more visibility.
It will be on Read-Only mode here.
<a href="https://discord.com/channels/811989166782021633/1278376975811219547/1278376975811219547">Join the conversation on Discord</a></i>
I’ve gone through this again with a new content type and its still not working, even with the CLI generating all the routes and stuff which I was missing before?
Any help on this please?
Is you plugin in a public repo, can help to look at all the code to trobule shoot.
<@960590891200970812> I managed to get past this by recreating the plugin with a different name. I think that if you delete a content-type from the main project, you cannot create a new content type or plugin with the same name. I’ve noticed issues like this before when trying to rename content-types
thats probably since models/content-types get stores in the database somewhere at strapi_core_store_settings
. I’ve had issues in the past as well with components that just wouldn’t go away because they were still in the DB
Your original file was also incorrect:
//./src/plugins/notifications/server/content-types/index.js
'use strict';
const task = require('./task');
module.exports = {
task
};
should be
//./src/plugins/notifications/server/content-types/index.js
'use strict';
const task = require('./task');
module.exports = {
task: { schema: task, lifecycles: ... }
};
I am going to try to reproduce it. I wonder if this is a bug, since should the plugin collection only be scoped to the plugin?
It felt like a bug to me. I think to reproduce - create a content-type in main project called notification, then delete that and create a plugin with name notification, and try and create a content-type within that called notification
I think this is a slightly different issue, but I’m having issues with the newly created content type. " TypeError: Error creating endpoint GET /pushkeys: Cannot read properties ││ of undefined (reading ‘find’) " with this route
module.exports = {
// webpushKey,
'content-api': {
type: 'content-api',
routes: [
{
method: 'GET',
path: '/pushkeys',
handler: 'pushkey.find',
config: {
policies: [],
middlewares: [],
prefix: "",
auth: false
},
},```
and this controller ```js
module.exports = ({ strapi }) => ({
async find(ctx) {
// some custom logic here
// calling the default core action with super
const { data, meta } = await super.find(ctx);
return { data, meta };
},
});```
and this content type
``` "kind": "collectionType",
"collectionName": "pushkeys",
"info": {
"singularName": "pushkey",
"pluralName": "pushkeys",
"displayName": "pushkey"
},```
Hi can you expand on this? In that file I’m only trying to import the schema for the conte-type. I haven’t read anything online that suggests I have to import everything at the point such as lifecycle methods etc?
Routes for content-type in plugin broken
you dont have to use lifecycles
but you should put task: { schema: task }
, not just export task
did you re-export this controller in the controllers/index.js?
const notification = require('./notification');
const settings = require('./settings');
const pushKey = require('./pushkey');
module.exports = {
notification,
settings,
pushKey
};
Are you sure this is necessary? It doesn’t seem to be an issue for my notification content-type to export like
'use strict';
const pushkey = require('./pushkey');
module.exports = {
"notification": require('./notification'),
"pushkey": { schema: pushkey }
};```
Not sure if its a step in the right direction, but the above gives the error
```│ TypeError: Cannot read properties of undefined (reading 'singularName') ││ at validateKeySameToSingularName (```
It has just occurred to me that I can possibly avoid this issue by adding fields directly to user when using the plugin. I’m pretty sure this is possible
change pushKey
to pushkey
the singularName error is because it tries to do schema.singularName
and you didn’t export notification as { schema: notification }
I actually have no idea how this fixed it (perhaps I changed a pushKey to pushkey I’d missed earlier, but I added a console log to strapi/dist/core/registries/content-types.js to have a look at the content types
"use strict";
const _ = require("lodash/fp");
const index = require("../domain/content-type/index.js");
const utils = require("../utils.js");
const validateKeySameToSingularName = (contentTypes) => {
for (const ctName of Object.keys(contentTypes)) {
console.log(contentTypes)```
and saw that the formatting of my 2 plugin content-types was weird
```js
{
notification: {
schema: {
schema: [Object],
kind: 'collectionType',
collectionName: 'notifications',
info: [Object],
options: [Object],
pluginOptions: {},
attributes: [Object],
__filename__: 'schema.json'
}
},
pushkey: { schema: { schema: [Object] } }
}
{
notification: {
schema: {
schema: [Object],
kind: 'collectionType',
collectionName: 'notifications',
info: [Object],
options: [Object],
pluginOptions: {},
attributes: [Object],
__filename__: 'schema.json'
}
},
pushkey: { schema: { schema: [Object] } }
}
I then changed content-types/index.js back to js module.exports = { "notification": require('./notification'), "pushkey": require('./pushkey'), };
and it started working