我可能会对这个问题感到失望。当然,我试图几乎同时学习所有这些技术并没有帮助。我使用Node.js / Express / Mongoose / MongoDB构建了一个API,它可以工作。我试图在Ember.js应用程序中使用此REST端点。 Ember应用程序使用夹具,因此客户端正在工作。
问题是REST端点将对象ID发送为_id
,而Ember期望并且需要id
。是的,我已经searched and located a related question并尝试了解决方案:
WbsEmber.ApplicationAdapter = DS.RESTAdapter.extend({
serializer: DS.RESTSerializer.extend({
primaryKey: function(type){
return '_id';
}
}),
host: 'http://localhost:8080',
namespace: 'api'
});
阅读Ember.DS api documentation for the RESTAdapter,看起来应该可行:
WbsEmber.ApplicationSerializer = DS.RESTSerializer.extend({
primaryKey: '_id'
});
WbsEmber.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8080',
namespace: 'api'
});
但是,两者都在控制台中不断抛出此错误堆栈。
Error while loading route: Error: No model was found for '0'
at new Error (native)
at Error.Ember.Error (http://localhost:9000/bower_components/ember/ember.js:910:19)
at Ember.Object.extend.modelFor (http://localhost:9000/bower_components/ember-data/ember-data.js:9805:33)
at JSONSerializer.extend.extractArray (http://localhost:9000/bower_components/ember-data/ember-data.js:3172:28)
at superWrapper (http://localhost:9000/bower_components/ember/ember.js:1292:16)
at Ember.Object.extend.extractFindAll (http://localhost:9000/bower_components/ember-data/ember-data.js:2380:21)
at Ember.Object.extend.extract (http://localhost:9000/bower_components/ember-data/ember-data.js:2365:37)
at http://localhost:9000/bower_components/ember-data/ember-data.js:10396:34
at invokeCallback (http://localhost:9000/bower_components/ember/ember.js:10013:19)
at publish (http://localhost:9000/bower_components/ember/ember.js:9683:9)
为了比较,我的夹具如下所示:
WbsEmber.Code.FIXTURES = [
{ id: 0, wbs: 'C04220', name: 'AGSS', type: 'prefix' },
{ id: 1, wbs: 'E5180', name: "ILO", type: 'suffix' },
{ id: 2, wbs: 'C04210', name: 'ARC1', type: 'prefix' },
{ id: 3, wbs: 'E0040', name: ' course preparation', type: 'suffix' },
{ id: 4, wbs: 'C04232', name: 'ARC2', type: 'prefix' },
{ id: 5, wbs: 'E0041', name: 'learn new course', type: 'suffix' },
{ id: 6, wbs: '4710', name: 'instructor labor', type: 'single' },
{ id: 7, wbs: 'ES1028', name: 'tech lead labor', type: 'single' }
];
来自REST端点的JSON响应如下所示:
[
{"_id": "53ac3f0d4e29d793d9f53384", "type": "prefix", "name": "UAGI", "wbs": "C04424", "__v": 0},
{"_id": "53ac3f114e29d793d9f53385", "type": "prefix", "name": "AGSS", "wbs": "C04220", "__v": 0},
{"_id": "53ac3f154e29d793d9f53386", "type": "prefix", "name": "ARC1", "wbs": "C04210", "__v": 0},
{"_id": "53ac3f184e29d793d9f53387", "type": "prefix", "name": "ARC2", "wbs": "C04232", "__v": 0}
]
直接从Chrome中的开发者工具复制和粘贴回复,因此请求成功完成并且响应正在向客户端发送。
仅供参考,以下是所有套餐和各自的版本。
"dependencies": {
"ember": "1.5.0",
"handlebars": "1.2.1",
"ember-data": "1.0.0-beta.7",
"bootstrap-sass-official": "3.1.1",
"ember-addons.bs_for_ember": "~0.7.0"
}
对于我的生活,我似乎无法弄清楚如何让这个工作。欢迎任何有关解决此问题的来源的建议,见解或参考。谢谢你的帮助。
答案 0 :(得分:1)
这是正确的模式:
WbsEmber.ApplicationSerializer = DS.RESTSerializer.extend({
primaryKey: '_id'
});
WbsEmber.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8080',
namespace: 'api'
});
您的json响应应该(或序列化),采用以下格式:
{
codes: [
{"_id": "53ac3f0d4e29d793d9f53384", "type": "prefix", "name": "UAGI", "wbs": "C04424", "__v": 0},
{"_id": "53ac3f114e29d793d9f53385", "type": "prefix", "name": "AGSS", "wbs": "C04220", "__v": 0},
{"_id": "53ac3f154e29d793d9f53386", "type": "prefix", "name": "ARC1", "wbs": "C04210", "__v": 0},
{"_id": "53ac3f184e29d793d9f53387", "type": "prefix", "name": "ARC2", "wbs": "C04232", "__v": 0}
]
}
转换文档对于仍在进行中的文档中的一些细微差别非常有帮助:https://github.com/emberjs/data/blob/master/TRANSITION.md