Access to payload object on plugin initialization #1018
-
Hi everyone, The reason: I'm trying to build a plugin, which requires some initial data (kind of default value) to be write into the auth collection for the very first time the plug in got enabled. I've checked the Any ideas appreciated, thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @khakimvinh , this is a great question. All config and plugin initialization is done before accessing the backend. If you need your plugin to perform some sort of action, your plugin could utilize the onInit: async (payload) => {
await payload.create({
collection: 'my-collection',
data: {
// Set field values here
},
});
}, Worth noting that if you populate this function inside of your plugin, any other Because this is a function and not a property that can be spread - what I'd recommend is having your plugin export an additional function that can be put into the main config's |
Beta Was this translation helpful? Give feedback.
Hey @khakimvinh , this is a great question. All config and plugin initialization is done before accessing the backend.
If you need your plugin to perform some sort of action, your plugin could utilize the
onInit
function of the config, which gives it access to the payload local API. Here is an example:Worth noting that if you populate this function inside of your plugin, any other
onInit
usage will likely be wiped out.Because this is a function and not a property that can be spread - what I'd recommend is having your plugin exp…