自定义cli生成的feathersjs服务

问题描述 投票:0回答:1

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

javascript mongoose feathersjs
1个回答
1
投票

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

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适配器)

© www.soinside.com 2019 - 2024. All rights reserved.