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

createRecord not defined error (Firebase + Ember.js)

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

更具体地说,这是我收到的错误:未捕获的类型错误:无法读取未定义的 属性 'createRecord'

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 不再引用控制器。您将需要使用 controllerContext.store.createRecord.

您可能刚刚失去了这里的上下文。 this 没有引用 controller,你在错误函数中。

有两种方法可以解决这个问题。首先是将函数绑定到控制器的 this:

ref.createUser({
  // ...
}, function(error, user) {
  var newUser = this.store.createRecord('user', {/*...*/});
  // ...
}.bind(this));

或重用 controllerContext 变量:

ref.createUser({
  // ...
}, function(error, user) {
  // ...
  var newUser = controllerContext.store.createRecord('user', {/*...*/});
});