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