【问题标题】:How does pluck hook work in feathersjs拔钩如何在feathersjs中工作
【发布时间】:2017-11-12 16:21:55
【问题描述】:

feathersjs 文档中提供的解释如下:

pluck 丢弃除指定字段之外的所有字段,无论是来自 提交的数据或来自结果的数据。如果数据是数组或 分页查找结果钩子将删除每个字段 项目。

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; 
  }; 
} 

getItems 实用程序返回 hook.data 或 hook.result 取决于钩子是用作之前还是 钩后。为查找返回 hook.result.data 或 hook.result 方法。

返回的项目总是一个数组以简化进一步的处理。

replaceItems 实用程序与 getItems 相反,返回 他们来自哪里。

我的问题与 checkContextIf 函数有关。这个函数防止 pluck 钩子被调用,除非在 create、update 和 patch 方法之前。那么 pluck 钩子如何对查询结果起作用。结果不是在服务调用后产生并在后挂钩中处理吗?

【问题讨论】:

    标签: hook feathersjs pluck


    【解决方案1】:

    如文档所述:

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

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

    • data - 请求数据(createupdatepatch

    【讨论】:

    • 谢谢。说 pluck 钩子不能用作后钩子是否正确,因为我理解 checkContextIf 函数会引发错误?还是 checkContextIf 函数说如果它是一个 before 钩子,那么它只能用于创建、更新和修补方法?
    • 也许可以在github.com/feathers-plus/feathers-hooks-common 中提交问题。你是对的,它应该适用于这两种情况(但它似乎没有)。
    • 很遗憾,我不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2020-09-18
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多