Javascript 自定义cli生成的feathersjs服务

Javascript 自定义cli生成的feathersjs服务,javascript,mongoose,feathersjs,Javascript,Mongoose,Feathersjs,我正在编写一个api来尝试featherjs及其mongoose适配器。我希望我的GET/books/端点只返回private属性设置为false的书籍。我应该先用钩子吗?如果是这样,我如何防止用户在我的端点中运行自定义查询?我是否应该手动清空参数对象?您需要在books.hooks.js const books_qry = require('../../hooks/books_qry'); module.exports = { before: { all: [], find:

我正在编写一个api来尝试featherjs及其mongoose适配器。我希望我的
GET/books/
端点只返回
private
属性设置为false的书籍。我应该先用钩子吗?如果是这样,我如何防止用户在我的端点中运行自定义查询?我是否应该手动清空
参数
对象?

您需要在
books.hooks.js

const books_qry = require('../../hooks/books_qry');

module.exports = {
  before: {
   all: [],
   find: [books_qry()],
   ...
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;
  }
}
创建
/src/hooks/books\u qry.js

const books_qry = require('../../hooks/books_qry');

module.exports = {
  before: {
   all: [],
   find: [books_qry()],
   ...
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适配器)

您需要在
books.hooks.js

const books_qry = require('../../hooks/books_qry');

module.exports = {
  before: {
   all: [],
   find: [books_qry()],
   ...
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;
  }
}
创建
/src/hooks/books\u qry.js

const books_qry = require('../../hooks/books_qry');

module.exports = {
  before: {
   all: [],
   find: [books_qry()],
   ...
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适配器)