如何将对象插入 Firebase Forge?- AngularFire 0.8.0 - Thinkster - 第 7 章 [英] How to insert object into Firebase Forge? - AngularFire 0.8.0 - Thinkster - Chapter 7
问题描述
我正在关注安装了最新版本 AngularFire (0.8.0) 的 Thinkster Ang-News 教程.我已经成功进入 第 7 部分 教程成功,但我被困在需要将用户对象插入 Firebase Forge 的部分.代码工作如下:
I am following the Thinkster Ang-News tutorial with the latest version of AngularFire (0.8.0) installed. I've managed to get to part 7 of the tutorial successfully but I am stuck on the part where I need to insert a user object into the Firebase Forge. The code works as follows:
当新用户通过我的 HTML 表单注册时,将调用注册"函数并调用User.create(authUser, $scope.user.username);"在注册"函数中执行.
When a new user registers via my HTML form, the "register" function is called and "User.create(authUser, $scope.user.username);" is executed within the "register" function.
// login and registration page
app.controller('AuthCtrl',
function ($scope, $location, Auth, User) {
if (Auth.signedIn()) {
$location.path('/');
}
$scope.$on('firebaseSimpleLogin:login', function(){
$location.path('/');
});
$scope.login = function(){
Auth.login($scope.user).then(function(){
$location.path('/');
}, function (error){
$scope.error = error.toString();
});
};
$scope.register = function () {
Auth.register($scope.user).then(function (authUser) {
Auth.login($scope.user);//cause creating user auto login
User.create(authUser, $scope.user.username);
console.log(authUser);
$location.path('/');
}, function (error){
$scope.error = error.toString();
});
};
});
用户"服务有一个创建"函数,它创建用户对象,并将对象信息存储在 Firebase Forge 中.正如您在下面看到的,users.$save(username)"被调用以将用户$save"到 Firebase Forge 中.但是,当调用 $save 方法时,我的 Firebase Forge 中没有任何反应.我查看了最新的 AngularFire 规范,正如您在我的代码注释 (//) 中看到的那样,我尝试将 $asArray()、$asObject() 与我的users"变量结合使用,但没有奏效.当我使用users.$add(username);"时我的 Firebase Forge 没有调用 $save 方法,而是添加了没有对象属性的用户名.
The "User" service has a function "create" which creates the user object and should store the object information in the Firebase Forge. As you can see below, "users.$save(username)" is called to "$save" the user into the Firebase Forge. However, nothing happens in my Firebase Forge when the $save method is called. I reviewed the latest AngularFire specs, and as you can see in my code comments (//), I tried combining the $asArray(), $asObject() with my "users" variable but that did not work. When I used "users.$add(username);" instead of calling the $save method, my Firebase Forge just added the username without the object's properties.
app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
//var users = $firebase(ref).$asObject();
//var users = $firebase(ref).$asArray();
var User = {
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username,
$priority: authUser.uid
};
//users.$add(username);
users.$save(username).then(function () {
setCurrentUser(username);
});
}, // end of create fxn
同样,我无法使用 $save 或 $add AngularFire 方法将users[username]"对象插入我的 Firebase Forge.如有任何帮助,我将不胜感激.
Again, I cannot insert the "users[username]" object into my Firebase Forge with the $save or $add AngularFire methods. I would greatly appreciate any help.
乔恩
推荐答案
现在您正在修改绑定,然后调用 $save
.您必须使用 $asObject
或 $asArray
来访问 $save
方法.
Right now you're modifying the binding and then calling $save
. You have to use $asObject
or $asArray
to get to the $save
method.
不过,在您的情况下,您不需要同步任何对象 - 只需保存一个.您可以为用户创建一个新对象并在绑定上使用 $update
.
In your case though, you don't need to sync any objects—just save one. You can create a new object for the user and use $update
on the binding.
app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
var User = {
create: function (authUser, username) {
users.$update(username, {
md5_hash: authUser.md5_hash,
username: username,
priority: authUser.uid
});
}, // end of create fxn
这篇关于如何将对象插入 Firebase Forge?- AngularFire 0.8.0 - Thinkster - 第 7 章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!