【问题标题】:Emitted Values Being Sent as One String Instead of Individual Values发出的值作为一个字符串而不是单个值发送
【发布时间】:2017-09-22 20:02:42
【问题描述】:

在我的 Angular 应用程序中,我通过 API 调用返回结果,并允许用户通过一系列复选框选择过滤这些结果。 Right now I'm running into an issue where, while results are returned as expected when one value is sent for a certain filter, when multiple values are selected (like filtering by more than one zipcode, for instance) I get zero results printed to风景。没有错误,只是没有结果。

在摸索了一会儿之后,使用 Chrome devtools 网络选项卡,我终于确定问题在于,而不是在有效负载中将每个项目用引号括起来 - 像这样:“90001”,“90002”,正在发送什么这是:“90001, 90002”。换句话说,引号被包裹起来,就好像它是一个值,而不是两个。

这是代码,我在其中使用“连接”将选定的值放在一起:

this.sendZipcode.emit(this.zipcodeFilters.text = arr.length === 0 ? undefined : arr.join(','));

我不确定如何调整这个“join”的构造方式,或者找到其他解决方案而不是“join”,以便将每个项目用引号括起来,而不是像一个长字符串一样包装。

仅供参考,这是我在选择两个邮政编码后在 Chrome devtools 的网络选项卡中看到的。正如我所解释的,它被包装成一个字符串,而不是两个值:

addresses.zipCode: {$in: ["90001, 90002"]}

【问题讨论】:

    标签: javascript mongodb angular mongoose


    【解决方案1】:

    Array.prototype.join 将返回一个字符串。因此,您将数组转换为单个字符串,该字符串作为字符串发送。你想发送一个数组。只需删除join 调用并直接返回arr

    字符串:“值,值”

    数组:“值”、“值”

    【讨论】:

    • 谢谢@ChrisG。我会试试看。
    • 所以,发送这样的东西? this.sendZipcode.emit(this.zipcodeFilters.text = arr.length === 0 ? undefined : arr);
    猜你喜欢
    • 2018-11-16
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多