elasticsearch,reactive,reactivesearch,Javascript,Reactjs,elasticsearch,Reactive,Reactivesearch" /> elasticsearch,reactive,reactivesearch,Javascript,Reactjs,elasticsearch,Reactive,Reactivesearch" />

Javascript 反应式搜索字段名称中的转义冒号

Javascript 反应式搜索字段名称中的转义冒号,javascript,reactjs,elasticsearch,reactive,reactivesearch,Javascript,Reactjs,elasticsearch,Reactive,Reactivesearch,我正在测试一个非常基本的实现 我的代码如下: 从“React”导入React,{Component}; 从'@appbaseio/reactivesearch'导入{ReactiveBase,DataSearch,ResultCard}; 从“/logo.svg”导入徽标; 导入“/App.css” class App extends Component { render() { return ( <div className="App"> &

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

我的代码如下: 从“React”导入React,{Component}; 从'@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;
在上面的例子中,当我调用像
res.image
这样的字段时,它会完美地工作。但是,
field\u product:title
field\u product:commerce\u price:amount
等字段返回错误,如下所示:

{
  "_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
}
语法错误:意外标记,应为,(34:41)


访问带有冒号的字段的正确方法是什么?有没有办法将其转义?

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

res['field_product:title']
这也是使用变量访问属性的方式:

const key = 'field_product:title';
res[key]
不过,最好更改json结构:

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

谢谢,成功了。索引部分是从另一个我无法控制的应用程序中完成的。因此,遗憾的是,对于JSON结构我无能为力。