Reactive Search 字段名称中的转义冒号

Escape colon in Reactive Search field name

我正在测试一个非常基本的 reactive search 实现。

我的代码如下: 从 'react' 导入 React,{ 组件}; 从 '@appbaseio/reactivesearch' 导入 { ReactiveBase, DataSearch, ResultCard }; 从 './logo.svg' 导入徽标; 导入'./App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>

        <ReactiveBase
            app="indexName"
            url="https://someurl.amazonaws.com"
          >

          <DataSearch
            componentId="SearchSensor"
            dataField={ "field_product:title" }
            autoSuggest={true}
          />    

          <ResultCard
             componentId="results"
             dataField="field_product"
             react={{
               "and": ["SearchSensor"]
             }}
             onData={(res)=>({
               "image": res.image,
               "title": res.field_product:title,
               "description":   res.description
             })}
           />

          </ReactiveBase>
      </div>
    );
  }

}

export default App;

我在索引中的字段如下所示:

{
  "_index": "kirana11",
  "_type": "product_autocomplete",
  "_id": "66641",
  "_version": 1,
  "_score": 1,
  "_source": {
    "id": 66641,
    "description": "Some Nestle Product ",
    "field_product:title": "Nestle Product",    
    "field_product:commerce_price:amount": 83
}

在上面的示例中,当我调用 res.image 之类的字段时,它可以完美地工作。但是,field_product:title & field_product:commerce_price:amount returns 等字段出现如下错误:

Syntax error: Unexpected token, expected, (34:41)

访问带冒号的字段的正确方法是什么?有办法逃脱吗?

用引号括起来并使用括号而不是点符号来访问 属性:

res['field_product:title']

这也是您使用变量访问 属性 的方式:

const key = 'field_product:title';
res[key]

虽然更改您的 json 结构可能会更好:

fieldProduct: {
  title: '',
  commercePrice: ...
}