node.js - 如何不将查询参数从角度发送到节点
问题描述
我想使用从选择选项中获取的查询参数搜索数据,如果它们为空,我不想将它们与请求一起发送。
角分量
genre = "";
platform = "";
queryparams = {
genre: this.genre,
platform: this.platform
}
public selectTopByPlaytime() {
this.gameService.selectTopByPlaytime(this.queryparams)
.pipe(takeUntil(this.unsubscribe$))
.subscribe(
(response) => {
//next() callback
console.log('response received')
console.log(response);
this.topGamesTime = response;
},
(error) => { //error() callback
console.error('Request failed with error')
this.errorMessage = error;
},
() => { //complete() callback
console.error('Request completed') //This is actually not needed
})
}
onChangeGenre(event: any) {
this.genre = event
this.queryparams.genre = this.genre
this.selectTopByPlaytime()
this.selectTopByUsers()
}
角服务
public selectTopByPlaytime(queryParams?): Observable<any> {
return this.http.get(this.baseUrl + "topbyplaytime", { params: queryParams }).pipe(retry(3), catchError(this.handleError));
}
如果参数为空或为空,我找不到停止发送参数的方法
在这里我如何从快递服务器中捕获它们:
const selectTopByPlaytime = catchAsync(async (req, res) => {
const options = req.query
try {
const data = await gameService.selectTopByPlaytime(options)
res.send(data)
} catch (error) {
res.status(400).json({ message: `${error}` })
}
})
const selectTopByPlaytime = async (options) => {
let topGames = games
console.log(options);
if (options.genre != undefined || options.genre != 'null') {
topGames = topGames.filter(game => game.genre == options.genre)
}
if (options.platform != undefined || options.platform != 'null') {
topGames = topGames.filter(game => game.platforms.includes(options.platform))
}
return _.chain(topGames)
.groupBy('game')
.map((value, key) => {
return {
'game': key,
'playTime': _.reduce(value, function (memo, i) { return memo + i.playTime; }, 0),
'genre': value[0].genre,
'platforms': value[0].platforms,
};
})
.sortBy((i) => { return -i.playTime; })
.first(5).value()
}
当我用邮递员测试我的 api 并设置参数时,一切正常,当我运行我的 angular 应用程序时,我无法发现问题是什么,数据总是空的!请问有什么帮助吗?
解决方案
尝试使用 Object.Assign(target, source)。它将属性从源对象复制到目标对象
欲了解更多信息链接
let queryparams = {};
if (this.genre) {
queryparams = Object.assign(queryparams, {genre: this.genre});
}
if (this.platform) {
queryparams = Object.assign(queryparams, {platform: this.platform});
}
推荐阅读
- c# - 枚举是局部静态变量的唯一替代品吗?
- javascript - 这会告诉我这个值是否在数组中吗?
- node.js - 对 Mongodb 中的投影执行不同的操作
- angular - 拦截器中的角度自定义标头
- safari - iOS Safari(移动)和 Angular Material UI:专注于输入字段时不需要的闪烁文本
- c++ - 如何检测两点之间是否有二进制图片中的黑色像素?C++
- python - Kivy 和 KivyMD 应用程序在启动 Android 时崩溃
- javascript - 从 Vue 组件中的对象道具初始化数据
- firebase - Flutter 和 Firebase | 无论何时验证用户电子邮件,如何动态显示不同的页面?
- angular - 标记两个可观察数组之间差异的最简单方法