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
3 Answers
Reset to default 4it'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