gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:37:29 25 4
gpt4 key购买 nike

我想知道是否有人可以指出正确的方向并帮助我修复我在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com