muxinc / strapi-plugin-mux-video-uploader

A Strapi plugin for managing uploads to Mux.
https://mux.com
23 stars 16 forks source link

Mux asset collection update not working through Strapi trigger based controller methods #46

Closed aagamLF closed 1 year ago

aagamLF commented 1 year ago

Hi,

I am trying to implement a afterCreate / afterUpdate trigger function on separate collection which has mux asset as a related collection in Strapi v3 controllers which would essentially set a flag in the Mux asset collection to true when called. Here's the code of what I am trying to do:

module.exports = {
    lifecycles: {
        // Called after an entry is created
        async afterCreate(result) {
            console.log(result);
            if (result.muxAsset && !result.muxAsset.linkedToVideoData) {
                const jsonData = {
                    'linkedToVideoData': true
                }
                const updateResult = await strapi.plugins['mux-video-uploader'].controllers['mux-asset'].update({ id: result.muxAsset.id }, jsonData);
                console.log('Marked video linked with mux asset ID: ', updateResult.id);
            }
        },
        // Called after an entry is updated
        async afterUpdate(result) {
            console.log(result);
            if (result.muxAsset && !result.muxAsset.linkedToVideoData) {
                const jsonData = {
                    'linkedToVideoData': true
                }
                const updateResult = await strapi.plugins['mux-video-uploader'].controllers['mux-asset'].update({ id: result.muxAsset.id }, jsonData);
                console.log('Marked video linked with mux asset ID: ', updateResult.id);
            }
        },
    },
};

Here's the error message we are seeing:

[2022-09-02T16:42:06.238Z] error TypeError: Cannot read property 'id' of undefined
    at C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:121:33
    at step (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:33:23)
    at Object.next (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:14:53)
    at C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:4:12)
    at Object.update (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-mux-video-uploader\controllers\mux-asset.js:116:38)
    at Object.afterCreate (C:\Users\AShah2\workspace\ContentManagement\api\custom-videos\models\custom-videos.js:19:106)
    at executeLifecycle (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-database\lib\utils\lifecycles.js:11:38)
    at async Object.create (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-database\lib\queries\helpers.js:18:3)
    at async Object.create (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi\lib\services\entity-service.js:142:17)
    at async C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-content-manager\controllers\collection-types.js:78:22
    at async C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-content-manager\utils\wrap-bad-request.js:5:5
    at async Object.create (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-content-manager\controllers\collection-types.js:77:5)
    at async C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi\lib\middlewares\router\utils\routerChecker.js:79:22
    at async module.exports (C:\Users\AShah2\workspace\ContentManagement\node_modules\strapi-plugin-content-manager\config\policies\routing.js:26:3)

I know that updating mux asset collection is not recommended. We have fixed that issue with our Strapi v4 implementation and will be launched soon. However, our current beta soft launch is going to be with Strapi v3 and this fix is essential for fixing an important bug.

Please guide us on what we might be missing in the code here.

Strapi: v3.6.8 strapi-plugin-mux-video-uploader: v1.2.0 local environment DB: sqlite v5.0.0 deployed environment DB: Postgres SQL v13.7

Thanks

erikpena commented 1 year ago

After speaking with @aagamLF about this further, this appears to have been a misunderstanding with the shape of the responses between v3 -> v4. In v3, responses are wrapped in a params property whereas in v4, they are flattened out. If this is still an issue or if there is anything else holding this back, please feel welcome to re-open this issue. Thanks!