return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Ember-data: "no model found" while it's there - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember-data: "no model found" while it's there - Stack Overflow

programmeradmin1浏览0评论

I'm trying to fetch model data from an RESTful backend. This works fine for the model "project", while for the model "canal" I only get an error message in the console saying:

Assertion failed: Error while loading route: Error: No model was found for '0'

Testing the API with curl works fine.

router.js:

App.Router.map(function() {
    this.route("start", { path: "/" });
    this.route("projects", { path: "/projects" });
    this.route("canals", { path: "/canals" });
});


App.ProjectsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('project');
    }
});

App.CanalsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('canal');
    }
});

models.js

App.Project = DS.Model.extend({
    title: DS.attr('string'),
    client: DS.attr('string'),
    ment: DS.attr('string'),
    project_number: DS.attr('string')
});

App.Canal = DS.Model.extend({
    pid:  DS.attr('number'),
    street: DS.attr('string'), 
    district: DS.attr('string'), 
    upper_inspection_point: DS.attr('string'), 
    lower_inspection_point: DS.attr('string'), 
    type: DS.attr('string'), 
    direction: DS.attr('string'), 
    profile: DS.attr('string'), 
    height: DS.attr('string'), 
    width: DS.attr('string'), 
    material: DS.attr('string'), 
    branch: DS.attr('number'), 
    length: DS.attr('number'),
    demolition_class: DS.attr('number'),
    date: DS.attr('date'),
    ment: DS.attr('string')
});

app.js

window.App = Ember.Application.create();
App.Store = DS.Store.extend({
    adapter: DS.RESTAdapter,
});
DS.RESTAdapter.reopen({
  namespace: 'api/index.php'
});

Dependencies/Versions

DEBUG: ------------------------------- ember-1.2.0.js:3231
DEBUG: Ember      : 1.2.0 ember-1.2.0.js:3231
DEBUG: Ember Data : 1.0.0-beta.5+canary.e1200068 ember-1.2.0.js:3231
DEBUG: Handlebars : 1.1.2 ember-1.2.0.js:3231
DEBUG: jQuery     : 2.0.3 ember-1.2.0.js:3231
DEBUG: ------------------------------- ember-1.2.0.js:3231
Ember Debugger Active 

Edit It seems, that the backend (SlimPHP) delivers inpatible JSON response (/). I updated fetchAll() to fetchAll(PDO::FETCH_ASSOC) but I still get the numbered index:

$sql = "SELECT * FROM canals WHERE pid = :pid";
        try {
            $db = getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam(":pid", $pid);
            $stmt->execute();
            $canals = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $db = null;
            header("Content-Type: application/json");
            echo json_encode($canals);
            exit;
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
            header("Content-Type: application/json");
            exit;
        }

I'm trying to fetch model data from an RESTful backend. This works fine for the model "project", while for the model "canal" I only get an error message in the console saying:

Assertion failed: Error while loading route: Error: No model was found for '0'

Testing the API with curl works fine.

router.js:

App.Router.map(function() {
    this.route("start", { path: "/" });
    this.route("projects", { path: "/projects" });
    this.route("canals", { path: "/canals" });
});


App.ProjectsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('project');
    }
});

App.CanalsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('canal');
    }
});

models.js

App.Project = DS.Model.extend({
    title: DS.attr('string'),
    client: DS.attr('string'),
    ment: DS.attr('string'),
    project_number: DS.attr('string')
});

App.Canal = DS.Model.extend({
    pid:  DS.attr('number'),
    street: DS.attr('string'), 
    district: DS.attr('string'), 
    upper_inspection_point: DS.attr('string'), 
    lower_inspection_point: DS.attr('string'), 
    type: DS.attr('string'), 
    direction: DS.attr('string'), 
    profile: DS.attr('string'), 
    height: DS.attr('string'), 
    width: DS.attr('string'), 
    material: DS.attr('string'), 
    branch: DS.attr('number'), 
    length: DS.attr('number'),
    demolition_class: DS.attr('number'),
    date: DS.attr('date'),
    ment: DS.attr('string')
});

app.js

window.App = Ember.Application.create();
App.Store = DS.Store.extend({
    adapter: DS.RESTAdapter,
});
DS.RESTAdapter.reopen({
  namespace: 'api/index.php'
});

Dependencies/Versions

DEBUG: ------------------------------- ember-1.2.0.js:3231
DEBUG: Ember      : 1.2.0 ember-1.2.0.js:3231
DEBUG: Ember Data : 1.0.0-beta.5+canary.e1200068 ember-1.2.0.js:3231
DEBUG: Handlebars : 1.1.2 ember-1.2.0.js:3231
DEBUG: jQuery     : 2.0.3 ember-1.2.0.js:3231
DEBUG: ------------------------------- ember-1.2.0.js:3231
Ember Debugger Active 

Edit It seems, that the backend (SlimPHP) delivers inpatible JSON response (http://jsfiddle/hk2ph/). I updated fetchAll() to fetchAll(PDO::FETCH_ASSOC) but I still get the numbered index:

$sql = "SELECT * FROM canals WHERE pid = :pid";
        try {
            $db = getConnection();
            $stmt = $db->prepare($sql);
            $stmt->bindParam(":pid", $pid);
            $stmt->execute();
            $canals = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $db = null;
            header("Content-Type: application/json");
            echo json_encode($canals);
            exit;
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
            header("Content-Type: application/json");
            exit;
        }
Share Improve this question edited Dec 31, 2013 at 15:29 lasagne asked Dec 31, 2013 at 11:56 lasagnelasagne 6412 gold badges11 silver badges23 bronze badges 1
  • possible duplicate of Ember 1.0.0 RESTAdapter failure – givanse Commented Aug 11, 2014 at 20:04
Add a ment  | 

3 Answers 3

Reset to default 4

it's your json, it's returning a property that's making ember data think there is a type 0, someting like this:

{
  canals:[
    {
      // stuff
    }],
  0: [
   {

   }]
}

The correct style for building the JSON is:

$canals = $stmt->fetchAll(PDO::FETCH_ASSOC);

Now everything works fine, thanks for support!

Check that your JSON api response is structured the way ember-data wants. You should provide all resource data structured like this:

{
  "items": [
    {
      "id": 0,
      "title": "foo"
    },
    {
      "id": 1,
      "title": "bar"
    }
  ]
}

Sources: Ember 1.0.0 RESTAdapter failure

发布评论

评论列表(0)

  1. 暂无评论