我正在尝试将当前用户加载到数据存储中,但遇到一些困难。服务器使用 PassportJS 并访问 /api/users/me
返回一个类似于以下内容的 JSON 对象:
{"user":{"_id":"53a4d9c4b18d19631d766980","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1f0d160a1110091f0c3e19131f1712501d1113" rel="noreferrer noopener nofollow">[email protected]</a>",
"last_name":"War","first_name":"Ashton","location":"Reading, England",
"birthday":"12/24/1993","gender":"male","fb_id":"615454635195582","__v":0}}
我的商店只是由 App.store = DS.Store.create(); 定义的
检索当前用户的 Controller 是:
App.UsersCurrentController = Ember.ObjectController.extend({
content: null,
retrieveCurrentUser: function() {
var controller = this;
Ember.$.getJSON('api/users/me', function(data) {
App.store.createRecord('user', data.user);
var currentUser = App.store.find(data.user._id);
controller.set('content', currentUser);
});
}.call()
});
它由我的应用程序 Controller 调用:
App.ApplicationController = Ember.Controller.extend({
needs: "UsersCurrent",
user: Ember.computed.alias("controllers.UsersCurrent")
});
我怀疑 App.store.createRecord('user', data.user);
行导致了问题,但我不知道如何修复它。
控制台记录TypeError: this.container is undefined
,而Ember调试器显示每个promise都已履行,并且users.current Controller 没有内容。感谢您提供的任何帮助。
最佳答案
您是否在 App
命名空间上定义存储,因为 Ember Data 默认情况下不会这样做。无论哪种方式,您都无法在创建记录后定义要查找的类型。
var currentUser = controller.store.find('user', data.user._id);
createRecord
返回记录,因此之后查找它是没有意义的
var currentUser = controller.store.createRecord('user', data.user);
此外,在您的示例中,您尝试立即在类型上调用该函数,而不是在实例上调用该函数。您应该将其添加为在 init 上运行的方法。
App.UsersCurrentController = Ember.ObjectController.extend({
retrieveCurrentUser: function() {
console.log('hello')
var controller = this;
Ember.$.getJSON('api/users/me', function(data) {
var user = controller.store.createRecord('user', data.user);
controller.set('model', user);
});
}.on('init')
});
关于ember.js - Ember-数据 "TypeError: this.container is undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24347864/