javascript - createRecord 未定义错误(Firebase + Ember.js)

标签 javascript ember.js firebase

我想知道是否有人可以指出正确的方向并帮助我修复我在使用 Firebases createUser 方法创建用户后尝试使用我的 Ember.js 模型添加用户时遇到的错误。

这里更具体的是我得到的错误:Uncaught TypeError: Cannot read property 'createRecord' of undefined

App.SignUpController = Ember.Controller.extend({
needs: ['sign-in'],
needs: ['application'],
userSignedIn: false,


actions: {
    signMeUp: function() {
        var state = false;
        var controllerContext = this;
        // Create firebase user
        ref.createUser({
                email    : this.get('email'),
                password : this.get('password'),
            }, function(error, user) {
                if (error === null) {
                    console.log('User created with id', user.uid);
                    state = true;
                    controllerContext.set('userSignedIn', state);
                    console.log("State from sign-up page: "+ state);

                    console.log("Testing user.uid inside: "+user.uid);
                    var fbid = user.id;
                    controllerContext.set('user id', user.uid);

                    var newUser = this.store.createRecord('user', {
                          id: fbid,
                          email: this.get('email'),
                          password: this.get('password'),
                    });
                    newUser.save();
            } else {
                    console.log("Error creating account:", error);
            }
        }); // End createUser

        this.transitionToRoute('letters');
    }
}
 });

更新:这是我在研究了一天的 JS 管道后想出的一个(非常老套的)解决方案。

App.SignUpController = Ember.Controller.extend({
needs: ['sign-in'],
needs: ['application'],
userSignedIn: false,
thisUserID: '',

actions: {
    signMeUp: function() {
        var state = false;
        var controllerContext = this;
        // Create firebase user
        function authWithPassCallback(userObj, user){
            console.log("authWithPassCallback user.uid is: "+user.uid);
            return user.uid

        }

        function createUserAndLogin(userObj, callback) {  
           ref.createUser(userObj, function(error, user) {
              if (error === null) {
                console.log("User created successfully");
                controllerContext.set('thisUserID', user.uid);
                return callback(userObj, user);

              } else {
                console.log("Error creating user:", error);
              }  
            }); 


        }

        var userAndPass = { 
            email: this.get('email'),
            password: this.get('password')}

        var fbPayload = createUserAndLogin(userAndPass, authWithPassCallback);

        setTimeout(function () { 
            console.log("FB load: "+ controllerContext.get('thisUserID'));
            var newUser = controllerContext.store.createRecord('user', {
                              id: controllerContext.get('thisUserID'),
                              email: controllerContext.get("email"),
                              password: controllerContext.get("password"),
            });
            newUser.save();
            controllerContext.transitionToRoute('letters');
       }, 1000);
        console.log(controllerContext.get('thisUserID'));

    }
}
});

最佳答案

我假设错误发生在 newUser = this.store.createRecord - 此时您的代码中的 this 不再引用 Controller 。您将需要使用 controllerContext.store.createRecord

关于javascript - createRecord 未定义错误(Firebase + Ember.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388947/

相关文章:

android - 带类映射器 : No setter/field for "class" found on class in android

javascript - 对象推送Firebase,如何从推送的项目中删除键名称

android - 如何在 Firebase 的 ValueEventListener() 的 onDataChange() 方法内添加到 ArrayList?

javascript - AngularJS 表单提交不起作用

javascript - Ember 在查看子路由时隐藏父模板

javascript - Angular 样本跟进

ember.js - Ember 组和顺序

javascript - Ember 模型对象 hasMany 没有像我期望的那样返回数组

javascript - ReplaceWith 不适用于函数内部

javascript - 从外部跨度获取文本,而不获取内部元素的文本