我有一个示例笔记本应用程序,可以使用ember-cli的HTTP模拟,也可以使用ActiveModelSerializer跟踪我的rails后端。
当我用ember-fire将它挂钩到firebase时,我能够注册一个用户(我在仪表板中看到它)但是当我尝试通过电子邮件检索它时,我收到以下警告:
WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using ui@serializer:application:.modelNameFromPayloadKey("0"))
那么这个错误:
Error: Assertion Failed: You must include an 'id' for undefined in an object passed to 'push'
at new Error (native)
at Error.EmberError (http://localhost:4200/assets/vendor.js:24735:21)
at assert (http://localhost:4200/assets/vendor.js:14636:13)
at Object.assert (http://localhost:4200/assets/vendor.js:22037:34)
at ember$data$lib$system$store$$Service.extend._pushInternalModel (http://localhost:4200/assets/vendor.js:75316:15)
at ember$data$lib$system$store$$Service.extend.push [as _super] (http://localhost:4200/assets/vendor.js:75302:34)
at push (http://localhost:4200/assets/vendor.js:94940:38)
at superWrapper [as push] (http://localhost:4200/assets/vendor.js:30984:22)
at http://localhost:4200/assets/vendor.js:70210:27
at Object.Backburner.run (http://localhost:4200/assets/vendor.js:9707:25)
我使用以下方式查询商店:
export default Ember.Route.extend({
actions: {
login: function() {
this.store.query('user', {
email: this.controller.get('email')
}).then((users) => {
if(users.get('length') === 1) {
var user = users.objectAt(0);
this.controllerFor('application').set('user',user);
this.transitionTo('notebooks', user.get('id'));
}
else {
console.log('unexpected query result');
}
});
}
}
});
深入挖掘,我可以通过在finders.js设置断点来看到#157我即将
store._adapterRun(function(){ var payload = normalizeResponseHelper(serializer,store,typeClass,adapterPayload,null,'query'); // TODO优化 records = store.push(payload); });
推送有效载荷。适配器有效负载检查
adapterPayload: Array[1]
0: Object
email: "test@test.com"
first_name: "Test"
id: "-K1oINClDw2ylQLww7-p"
last_name: "User"
这是我的用户。所以除了跟踪之外一切都很好。不确定该ID,但我是firebase的新手;也许没关系。它符合我在仪表板中看到的内容。
我没有对我的序列化器做过任何特别的事 - 它是香草。
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
});
AFAIK我正在使用最新的&最大的 - 这是bower.json
{
"name": "ui",
"dependencies": {
"ember": "2.1.0",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.4",
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
"ember-data": "2.1.0",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.1.7",
"ember-qunit": "0.4.9",
"ember-qunit-notifications": "0.0.7",
"ember-resolver": "~0.1.18",
"jquery": "^2.1.4",
"loader.js": "ember-cli/loader.js#3.2.1",
"qunit": "~1.18.0",
"foundation": "~5.5.3",
"bootstrap": "~3.3.5",
"showdown": "~1.3.0",
"firebase": "^2.1.0"
}
}
和package.json中的dev依赖项
"devDependencies": {
"body-parser": "^1.14.1",
"broccoli-asset-rev": "^2.1.2",
"ember-cli": "1.13.8",
"ember-cli-app-version": "1.0.0",
"ember-cli-babel": "^5.1.3",
"ember-cli-dependency-checker": "^1.0.1",
"ember-cli-htmlbars": "1.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-ic-ajax": "0.2.1",
"ember-cli-inject-live-reload": "^1.3.1",
"ember-cli-qunit": "^1.0.0",
"ember-cli-rails-addon": "0.0.12",
"ember-cli-release": "0.2.3",
"ember-cli-showdown": "2.5.0",
"ember-cli-sri": "^1.0.3",
"ember-cli-uglify": "^1.2.0",
"ember-data": "2.1.0",
"ember-disable-proxy-controllers": "^1.0.0",
"ember-export-application-global": "^1.0.3",
"emberfire": "1.6.0",
"express": "^4.13.3",
"glob": "^4.5.3",
"morgan": "^1.6.1",
"nedb": "^1.2.1"
}
任何指针/帮助/指导都会很棒!我也是新来的,所以也许我错过了显而易见的事情?
答案 0 :(得分:0)
您需要从后端返回一个类型键,而不是仅发送一个数组。这让Ember Data知道您推送到商店的模型类型。您的有效负载应如下所示:
users: [
{
email: "test@test.com",
id: "abcdefg"
(...)
}
]
而不是
[
{
email: "test@test.com",
id: "abcdefg"
(...)
}
]
或者,您可以明确传入类型和数据:
store.push("user", store.normalize("user", response[0]));
(响应仍然是数组)