javascript – Ember-data:“没有找到模型”,而它就在那里
作者:互联网
我正在尝试从RESTful后端获取模型数据.这适用于模型“项目”,而对于模型“canal”我只在控制台中收到一条错误消息:
Assertion failed: Error while loading route: Error: No model was found
for ‘0’
使用curl测试API工作正常.
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'
});
依赖/版本
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
编辑
看来,后端(SlimPHP)提供了不兼容的JSON响应(http://jsfiddle.net/hk2ph/).我将fetchAll()更新为fetchAll(PDO :: FETCH_ASSOC),但我仍然得到编号索引:
$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;
}
解决方法:
它是你的json,它正在返回一个属性,它使得ember数据认为有一个类型0,有点像这样:
{
canals:[
{
// stuff
}],
0: [
{
}]
}
标签:javascript,php,rest,ember-js,ember-data 来源: https://codeday.me/bug/20190825/1717693.html