【问题标题】:Modify every data response in strapi修改strapi中的每个数据响应
【发布时间】:2021-06-30 07:38:01
【问题描述】:
我想知道在从 api 返回任何数据之前执行函数的最佳方法是什么,以便我可以修改数据响应。
我知道我可以将功能添加到每个控制器,但这意味着重复自己很多次。策略似乎是正确的做法,但它们会在控制器被击中之前执行。
有人知道怎么做吗?
module.exports = {
async find(ctx) {
let entities;
ctx.resp... // altering the data this way at the moment
【问题讨论】:
标签:
reactjs
api
next.js
content-management-system
strapi
【解决方案1】:
可以在控制器函数执行后执行策略,代码必须在 await next() 之后执行,如下所示。
module.exports = async (ctx, next) => {
// Indicate to the server to go to
// the next policy or to the controller's action.
await next();
// The code below will be executed after the controller's action.
if (ctx.status === 404) {
ctx.body = 'We cannot find the resource.';
}
};
请参考:Advanced usage