# 商店中水合查找结果时可能存在 Bug

**URL:** https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394
**Category:** Development
**Created:** [2018年七月15日 14:57 UTC](https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394 "2018-07-15T14:57:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [2018年七月15日 14:57 UTC](https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394/1 "2018-07-15T14:57:21Z")

</div>

What is the following line doing: [discourse/app/assets/javascripts/discourse/models/store.js.es6  
 ](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/models/store.js.es6#L98)?

```js
return this._hydrate(type, result[Ember.String.underscore(type)], result);

```

In particular, why is the second argument of the `_hydrate()` method not the `result` object? Usually, the second argument for this method is the record object that is saved to/retrieved from the store.

Here, when I’m trying to find a record by ID, I will always run into this line:

```js
if (!obj) {
  throw new Error("Can't hydrate " + type + " of `null`");
}

```

That’s because `_hydrate()` will be called with `result[Ember.String.underscore(type)]`. Since my record doesn’t have a property named like the `type`, that’s always `undefined`.

Is that a bug?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [2018年七月16日 16:27 UTC](https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394/3 "2018-07-16T16:27:43Z")

</div>

We require when returning data from the server that the object is not the root of the JSON object. For example instead of:

`{ "id": 123, "name": "robin"}` it should be `{"person": {"id": 123, name: "robin"} }`

This is used for cross-referencing records. For example a JSON payload might include multiple relationships and the store will wire them up together.

If you have no control over your server API, you can create a new adapter to transform the returned JSON into the format our code requires.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [2018年七月17日 14:13 UTC](https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394/4 "2018-07-17T14:13:18Z")

</div>

@eviltrout If I understand this correctly, for an endpoint `plugin-route`, the method `this.store.find('plugin-route')` would return a response with a `plugin_route` property (singular) in the `responseJson` property, but the method `this.store.findAll('plugin-route')` would return a response with a `plugin_routes` property (plural) in the `responseJson` property?

Likewise, sending a request for `/persons`, a response object with a property `persons` in `responseJson` is expected, correct?

```json
{
  "persons": []
}

```

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [2018年七月17日 15:14 UTC](https://meta.discourse.org/t/potential-bug-in-store-when-hydrating-find-results/92394/5 "2018-07-17T15:14:17Z")

</div>

Yes that is the idea. You can review a bunch of examples in our test suite:

[https://github.com/discourse/discourse/blob/master/test/javascripts/helpers/store-pretender.js.es6](https://github.com/discourse/discourse/blob/master/test/javascripts/helpers/store-pretender.js.es6)
