2

我正在测试一个非常基本的反应式搜索实现。

我的代码如下: import React, { Component } from '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返回如下错误:

语法错误:意外令牌,预期,(34:41)

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

4

1 回答 1

1

Wrap it around quotes and use bracket instead of dot notation to access the property:

res['field_product:title']

This is also how you would access a property using a variable:

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

It might be better to change your json structure though:

fieldProduct: {
  title: '',
  commercePrice: ...
}
于 2018-05-29T00:12:27.227 回答