0

无法从我的模型中找到记录

灰烬版本:

DEBUG: -------------------------------
DEBUG: Ember      : 1.0.0
DEBUG: Ember Data : 1.0.0-beta.2 
DEBUG: Handlebars : 1.0.0 ember.js
DEBUG: jQuery     : 1.9.1 ember.js
DEBUG: ------------------------------- 

模型:

App.Concert = DS.Model.extend({
    tour_id: DS.attr(),
    tickets: DS.attr()
});

发现:

this.get("store").find("concert", {tour_id: 1}).then(function(result) {
            console.log("--------------");
            console.log(result.content.length);
            console.log("--------------");
        }, function(error) {
            console.log("broken");
        });

安慰:

------------- 
length: 0 
-------------- 

我无法按属性从我的模型中找到记录。有人可以帮忙吗?我知道有 tour_id: 1 的记录存在

4

2 回答 2

1

Edu is actually partially correct (see below), but the real reason that you don't get back any records is most likely the format that your API returns. Ember Data is very strict about the type of data it expects and how it is structured. If you query your API for concerts with a tour_id of 1, then this is how the response should look like. Notice that the root object is the pluralized model name and the key is an array of concerts.

{
  "concerts": [
    {
      "id": 1,
      "tour_id": 1,
      "name": "cool concert"
    },
    {
      "id": 5,
      "tour_id": 1,
      "name": "awesome concert"
    }
  ]
}

Regarding Edu's comment, if you invoke the store's find method, Ember Data doesn't perform a regular find. Under the hood, findQuery is invoked and it returns an instance of DS.RecordArray. This isn't a regular JavaScript array so you need to respect how Ember works and how it deals with arrays.

I recommend reading up on DS.RecordArray in the Ember guides. Also, if you plan on using Ember Data, you shouldn't be scared by the idea of exploring its source. Ember Data is a ambitious project and the documentation is still very much a work in progress. Ember Data's source is therefore your most important source of information.

于 2013-12-20T19:35:56.790 回答
0

尝试使用吸气剂...

console.log(result.get('length'));
于 2013-10-08T11:02:37.787 回答