node.js - 反应表。如何进行服务器端过滤。请求参数
问题描述
我正在使用反应表和服务器端过滤。问题是过滤时过滤器不能一起应用。例如,如果我按名称和按类别过滤,则名称过滤器将重置。
网址如下所示:
http://localhost:3001/api/v1/products?pages=0&pageSize=20&filtered[]=%7B%22id%22:%22name%22,%22value%22:%22j%22%7D&filtered[]=%7B%22id%22:%22comment%22,%22value%22:%22f%22%7D".
我不知道filter[]=
每次尝试按不同列过滤时调用是否正常,或者服务器端是否存在问题。
这是控制器:
exports.index = function (req, res) {
let filtered = {}
let filters = {}
if (req.query.filtered) {
req.query.filtered.map(result => {
filtered = JSON.parse(result)
})
let id = filtered.id
let value = filtered.value
if (filtered['id'] === 'name' || filtered['id'] === 'comment') {
filters[id] = { '$regex': value, '$options': 'i' }
} else {
filters[id] = value
}
}
Product.listWithCategory(filters).then(response => {
res.json({ result: response })
})
}
反应 onFetchData:
onFetchData = (state, instance) => {
const jwt = getJwt()
if (!jwt) {
this.props.history.push('/login')
}
console.log(state.filtered)
let config = {
headers: { 'Authorization': `Bearer ${jwt}` },
params: {
pages: state.page,
pageSize: state.pageSize,
sorted: state.sorted,
filtered: state.filtered
}
}
this.setState({ loading: true })
axios.get('http://localhost:3001/api/v1/products', config)
.then(response => {
this.setState({
data: response.data.result,
pages: response.data.pages,
loading: false
})
})
}
解决方案
推荐阅读
- javascript - 使用 JavaScript 设置 Content-Transfer-Encoding 标头
- docker - 如何使用密码保护 Docker 镜像或容器?
- windows - 访问被拒绝调用 EnumJobs
- python-3.x - 如何重定向 multiprocessing.Process 的标准输出
- keras - Keras 功能 API:是否可以在分支模型上以分支方式阻止训练?
- android - 用手指画线android
- regex - 如何编写匹配多种模式中的任何一种的正则表达式?
- python - 如何将一行两列数据框转换为多行两列数据框
- javascript - 将日期时间表单 Net 转换为 JavaScript 在 IE 上不起作用
- ruby-on-rails - 模型助手中的未定义方法错误