I’m trying to perform CRUD operations with EntityService API inside my plugin.
I created a plugin using strapi generate
command.
Then create an index.js file under /src/plugins/myplugin/admin/src/components/myapp/index.js
with the below content
import React from "react";
import { Button } from "@strapi/design-system/Button";
import Refresh from "@strapi/icons/Refresh";
import { useCMEditViewDataManager } from "@strapi/helper-plugin";
export const Myapp = () => {
const { modifiedData } = useCMEditViewDataManager();
const handleConfirm = async (id) => {
try {
alert(id);
const res = await strapi.entityService.findOne("api::show.show", id);
console.log(res);
} catch (error) {
console.log(error);
}
};
return (
<>
<Button
variant="secondary"
startIcon={<Refresh />}
onClick={() => handleConfirm(modifiedData.id)}
>
Sync
</Button>
</>
);
};
Then pointed this file in the root of the admin directory’s index.js file
bootstrap(app) {
app.injectContentManagerComponent("editView", "right-links", {
name: "myapp",
Component: Myapp,
});
},
This button is appearing as expected in the edit view right sidebar. But when I click on it, I’m getting the following error. So I can’t perform any action with Entity Service API
TypeError: Cannot read properties of undefined (reading 'findOne')
at Sync (index.js?591e:12:31)
at eval (index.js?8896:25:29)
at Generator.next (<anonymous>)
at eval (23853:36:61)
at new Promise (<anonymous>)
at __async (23853:20:10)
at handleConfirm (index.js?8896:22:39)
at onClick (index.js?8896:41:24)
at handleClick (VM293 Button.js:76:7)
at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:3945:1)