简体   繁体   English

如何删除 <br> GraphQL查询结果中的字符串标记

[英]How to remove <br> tags from string in GraphQL query results

I'm working on a React application that utilizes Apollo and GraphQl to query an external API. 我正在研究一个利用Apollo和GraphQl查询外部API的React应用程序。 My issue is that the data contains strings that have 我的问题是数据包含的字符串
tags in them. 其中的标签。 I'd like to remove the 我想删除
tags. 标签。

The string looks like: 字符串看起来像:

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. 

My response data looks like 'data.loans.lend.values', and so I've tried using the str.replace() method on my data. 我的响应数据看起来像'data.loans.lend.values',所以我尝试在我的数据上使用str.replace()方法。 However, it didn't work. 但是,它没有用。 I've probably spent about five hours combing through the web to find a solution, but haven't been able to. 我可能花了大约五个小时梳理网络来找到解决方案,但却无法做到。

This is what my Apollo query component looks like. 这就是我的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 (

And this is my GraphQL query. 这是我的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
      }
    }
  }
}`

Has anyone else encountered this issue before? 有没有其他人遇到过这个问题?

if you are receiving data back in a format you don't like, this means the graphql server, a database it leverages, or an api it leverages, is returning data to the graphql server in a format that isn't useful for you. 如果您以不喜欢的格式接收数据,这意味着graphql服务器,它利用的数据库,或者它利用的API,正在以对您无用的格式将数据返回到graphql服务器。 The other possibility is that your server itself is formatting your data in an annoying way. 另一种可能性是您的服务器本身正在以恼人的方式格式化您的数据。 So here are your options: 所以这是你的选择:

  1. change your server / data sources as appropriate 根据需要更改您的服务器/数据源
  2. do a global replace on the string returned: str.replace(/<b>/g, '').replace(/<\\/b>/g, '') . 对返回的字符串执行全局替换: str.replace(/<b>/g, '').replace(/<\\/b>/g, '') Double check my escape characters, I may have that backwards. 仔细检查我的逃脱角色,我可能会倒退。

in a string replace, /[what you want replaced]/g = a regex for global detection, across the entire string 在字符串替换中,/ [你想要替换的东西] / g =整个字符串中用于全局检测的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM