Commit 7c558c2
Merge #322
322: Add objectID when primaryKey is provided r=bidoubiwa a=bidoubiwa
## Problem
With the new version of `instantSearch`, `objectID` becomes mandatory in the hits information to avoid errors and warnings in the console.
For example, in `React` when using instant-meilisearch the following error is raised:
```
react_devtools_backend.js:2430 Warning: Failed prop type: The prop `hits[0].objectID` is marked as required in `Hits`, but its value is `undefined`.
react_devtools_backend.js:2430 Warning: Each child in a list should have a unique "key" prop.
```
Because instantSearch adds the `objectID` as the key of every hit component, we both receive an error for its absence and an error because our components are missing a key prop as [react expects a key on every component](https://reactjs.org/docs/lists-and-keys.html#keys).
## Solution
First I tried to fetch the primaryKey from MeiliSearch but the public key does not give permission to fetch the primaryKey.
So the solution was to add the `primaryKey` option to instant-meilisearch options. If it is not present, the `objectID` field will not be added in the returned hits. If a wrong primaryKey is given its value will be undefined and the error above will be raised the same way.
### Usage
```javascript
const searchClient = instantMeiliSearch(
'https://demos.meilisearch.com',
'dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25',
{
paginationTotalHits: 60,
primaryKey: 'id',
}
)
```
Not breaking!
Co-authored-by: Charlotte Vermandel <[email protected]>4 files changed
+7
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| 86 | + | |
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| 103 | + | |
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| |||
0 commit comments