【问题标题】:how to do not send query params from angular to node如何不将查询参数从角度发送到节点
【发布时间】:2021-08-22 12:33:27
【问题描述】:

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

角度分量

  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.js angular api parameters request


    【解决方案1】:

    尝试使用 Object.Assign(target, source)。它将属性从源对象复制到目标对象

    欲了解更多信息link

    let queryparams = {};
    
    if (this.genre) {
          queryparams = Object.assign(queryparams, {genre: this.genre});
        }
    
    if (this.platform) {
          queryparams = Object.assign(queryparams, {platform: this.platform});
        }
    
    
    

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 2019-09-07
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 2020-09-17
      • 2014-09-26
      • 2012-12-07
      • 1970-01-01
      相关资源
      最近更新 更多