History of content updation in Strapi

Strapi is a Javascript-based, next-generation, open-source headless CMS. It provides developers the facility to use their favorite frameworks and tools to create, modify and expose to any digital device. it also saves the development of Back-end API.

During the development of a project, one of the admins updated a few important records wrongly and we were unable to recognize who has done it. So I started finding out a way to fetch the record of the user responsible for the last modification on a particular content. And below is the block of code that the developer needs to add in the code block.

Go to the file ./api/yourmodelname/models/yourmodelname.js and add this block of code there.

[JavaScript] module.exports = { lifecycles: { afterUpdate: async (modifications, filter, resultData) => { console.log(modifications.updated_by) // Full user object of editing Strapi user console.log(resultData.updated_by) // User ID of editing Strapi user }, }, }; [JavaScript]

Once you deploy this change, every time when anyone modifies the content, a JSON will be printed in the log like below.

[JSON] updated_by: { isActive: true, blocked: false, roles: [ 611b60adddf2350662048bf9 ], _id: 6321919f44ba7a7deb7d6289, username: null, registrationToken: null, firstname: ‘Rohit’, lastname: ‘Ranjan’, email: ‘rohit.ranjan@example.com’, __v: 0, password: ‘$2a$10$1RlJ9uRzL3Ka/0lTxPXHSeXZkyBd5yhMTpWPqokoLJ4ubh0rmt47C’, id: ‘6321919f44ba7a7deb7d6289’ } [JSON]

Once you deploy this change, every time when anyone modifies the content, a JSON will be printed in the log like below.

[JSON] updated_by: { isActive: true, blocked: false, roles: [ 611b60adddf2350662048bf9 ], _id: 6321919f44ba7a7deb7d6289, username: null, registrationToken: null, firstname: ‘Rohit’, lastname: ‘Ranjan’, email: ‘rohit.ranjan@example.com’, __v: 0, password: ‘$2a$10$1RlJ9uRzL3Ka/0lTxPXHSeXZkyBd5yhMTpWPqokoLJ4ubh0rmt47C’, id: ‘6321919f44ba7a7deb7d6289’ } [JSON]

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

JavaScript Objects

Introduction JavaScript is a versatile and flexible programming language, and one of its most powerful features is its ability to work with objects. Objects are...

Error Handling in React Js

Errors are bound to happen in our applications, whether they’re server-related errors, edge cases, api calling issues or others. As such, many methods have...

Understanding Different Ways of Event Handling in Javascript

In this Article we will understand the Different ways of Event handling that Javascript offers to us, but first, we need to understand what...

LEAVE A REPLY

Please enter your comment!
Please enter your name here