我正在测试一个非常基本的reactive search实现 .
我的代码如下:从'react'导入React,;从'@ appbaseio / reactivesearch'导入{ReactiveBase,DataSearch,ResultCard};从'./logo.svg'导入徽标; import'./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)
访问带冒号的字段的正确方法是什么?有办法逃脱它吗?
1 回答
将其包裹在引号周围并使用括号而不是点符号来访问属性:
这也是您使用变量访问属性的方式:
虽然改变你的json结构可能会更好: