首页 > 解决方案 > 如何不将查询参数从角度发送到节点

问题描述

我想使用从选择选项中获取的查询参数搜索数据,如果它们为空,我不想将它们与请求一起发送。

角分量

  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 应用程序时,我无法发现问题是什么,数据总是空的!请问有什么帮助吗?

标签: node.jsangularapiparametersrequest

解决方案


尝试使用 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});
    }



推荐阅读