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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

As the documentation states:

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 is the data (body) sent with a 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)
Daff
  • 43,734
  • 9
  • 106
  • 120
  • thanks. Is it correct to say that the pluck hook cannot be used as an after hook since it is my understanding that the checkContextIf function will raise an error? or Is the checkContextIf function saying that if it is a before hook then it must be used only for create, update and patch methods? – AlokJoshiOfAarmax Nov 14 '17 at 14:45
  • Maybe submit an issue in https://github.com/feathers-plus/feathers-hooks-common. You are right, that it should work for both cases (but it seems it doesn't). – Daff Nov 14 '17 at 17:55
  • Unfortunately, I do not know how to do that. – AlokJoshiOfAarmax Nov 15 '17 at 04:40