我正在开发一个 React 应用程序,该应用程序利用 Apollo 和 GraphQl 来查询外部 API。我的问题是数据包含带有
标签的字符串。我想删除
标签。
字符串看起来像:
Additionally, Igor works as a driver for a transport company.<br /><br />The spring agricultural works on the land started already and he now supports more expenses.
我的响应数据看起来像“data.loans.lend.values”,因此我尝试对我的数据使用 str.replace() 方法。然而,这并没有奏效。我可能花了大约五个小时在网络上查找解决方案,但一直未能找到。
这就是我的 Apollo 查询组件的样子。
<Query query={kivaLoans} variables={{ country: country, sortBy: sort, limit: limitResults }}>
{({ data, loading, error }) => {
if (loading) return <div><p>Loading...</p></div>;
if (error) return <p>ERROR</p>;
return (
这是我的 GraphQL 查询。
gql`
query ($country: [String], $sortBy: LoanSearchSortByEnum, $limit: Int) {
lend {
loans(filters: {status: fundraising, country: $country}, sortBy: $sortBy, limit: $limit) {
values {
id
plannedExpirationDate
image {
url(customSize: "s900")
}
name
loanFundraisingInfo {
fundedAmount
}
loanAmount
description
loanAmount
}
}
}
}`
之前有其他人遇到过这个问题吗?
最佳答案
如果您以不喜欢的格式接收返回的数据,这意味着 graphql 服务器、它利用的数据库或它利用的 api 正在以无用的格式将数据返回到 graphql 服务器为你。另一种可能性是您的服务器本身正在以一种烦人的方式格式化您的数据。所以这是你的选择:
- 根据需要更改您的服务器/数据源
- 对返回的字符串进行全局替换:
str.replace(/<b>/g, '').replace(/<\/b>/g, '')
。仔细检查我的转义字符,我可能把它弄反了。
在字符串替换中,/[要替换的内容]/g = 用于全局检测的正则表达式,跨整个字符串
关于javascript - 如何从 GraphQL 查询结果中的字符串中删除 <br> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030920/