【问题标题】:Customize cli-generated feathersjs services自定义 cli 生成的 feathersjs 服务
【发布时间】:2019-03-12 08:16:41
【问题描述】:

我正在编写一个 api 来尝试使用它的猫鼬适配器来尝试使用 featherjs。我希望我的 GET /books/ 端点只返回 private 属性设置为 false 的书籍。我应该使用 before 钩子吗?如果是这种情况,如何防止用户在我的端点中运行自定义查询?我应该手动清空params 对象吗?

【问题讨论】:

    标签: javascript mongoose feathersjs


    【解决方案1】:

    你需要在books.hooks.js中创建一个before钩子

    const books_qry = require('../../hooks/books_qry');
    
    module.exports = {
      before: {
       all: [],
       find: [books_qry()],
       ...
    

    创建/src/hooks/books_qry.js

    module.exports = function () {
      return function (context) {
         //You have 2 choices to change the context.params.query
    
         //overwrite any request for a custom query
         context.params.query =  { private: false };
    
         //or add a query param to the request for a custom query
         context.params.query.private = false
    
         //check the updated context.params.query 
         console.log(context.params.query); 
    
         return context;
      }
    }
    

    选择您需要的选项之一。由于我目前从未使用过 mongoose,请查看文档以创建有效查询(顺便说一下,上面的示例适用于 mongodb 适配器)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多