Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'),
    comment: 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'),
    comment: 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 incompatible JSON response (http://jsfiddle.net/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;
        }
like image 630
lasagne Avatar asked Jul 29 '26 06:07

lasagne


2 Answers

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: [
   {

   }]
}
like image 134
Kingpin2k Avatar answered Jul 31 '26 20:07

Kingpin2k


The correct style for building the JSON is:

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

Now everything works fine, thanks for support!

like image 24
lasagne Avatar answered Jul 31 '26 20:07

lasagne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!