0

In the feathersjs docs the explanation provided is as follows:

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.

My question relates to the checkContextIf function. This function prevents the pluck hook from being called except before the create,update and patch methods. How then does the pluck hook work on the results of the query. Are not the results produced after the service call and handled in an after hook?

4

1 回答 1

0

正如文档所述:

getItems 实用程序返回 hook.data 或 hook.result 中的项目,具体取决于挂钩是用作前挂钩还是后挂钩。

hook.data是与createpatchupdate请求一起发送的数据(正文) ,因此可用于省略您不想保存到数据库中的字段。这也记录在hooks API中:

  • data- 请求数据(对于create和)updatepatch
于 2017-11-14T01:50:51.170 回答