羽毛钩如何在 feathersjs 中工作

How does pluck hook work in feathersjs

在feathersjs文档中给出的解释如下:

pluck discards all fields except for the specified ones, either from the data submitted or from the result. If the data is an array or a paginated find result the hook will remove the field(s) for every item.

import _pluck from '../common/_pluck'; 
import checkContextIf from './check-context-if'; 
import getItems from './get-items'; 
import replaceItems from './replace-items'; 

export default function (...fieldNames) { 
  return context => { 
    checkContextIf(context, 'before', ['create', 'update', 'patch'], 'pluck'); 
    if(context.params.provider) { 
      replaceItems(context, _pluck(getItems(context), fieldNames)); 
    } 

    return context; 
  }; 
} 

The getItems utility returns the items in either hook.data or hook.result depending on whether the hook is being used as a before or after hook. hook.result.data or hook.result is returned for a find method.

The returned items are always an array to simplify further processing.

The replaceItems utility is the reverse of getItems , returning the items where they came from.

我的问题与 checkContextIf 函数有关。这个函数防止 pluck hook 在创建、更新和补丁方法之前被调用。 pluck 钩子如何处理查询结果。结果不是在服务调用后产生并在 after hook 中处理的吗?

如文档所述:

The getItems utility returns the items in either hook.data or hook.result depending on whether the hook is being used as a before or after hook.

hook.data 是用 create, patch or update request so it can be used to omit fields that you do not want to be saved to the database. This is also documented in the hooks API:

发送的数据(正文)
  • data - The request data (for create, update and patch)