$ save() 문제에 대한 다른 게시물을 보았지만이를 내 코드와 관련시킬 수 없습니다. 궁극적으로 변경된 후에 새 데이터를 데이터베이스에 저장하려고 시도하는 updateProfile() 메서드가있는 프로필 컨트롤러가 있습니다. 다음과 같이 내 프로필 컨트롤러를 정의
:
angular.module('angularfireSlackApp')
.controller('ProfileCtrl', function($state, md5, profile) {
var profileCtrl = this;
var currentUser = firebase.auth().currentUser;
profileCtrl.profile = profile;
// Retrieves the user's email from input field, hashes it, and saves the data to the database
profileCtrl.updateProfile = function() {
console.log(profileCtrl.profile); // Profile object exists and is populated as expected
profileCtrl.profile.emailHash = md5.createHash(currentUser.email);
profileCtrl.profile.$save();
}
});
내 사용자 서비스 :
.state('profile', {
url: '/profile',
controller: 'ProfileCtrl as profileCtrl',
templateUrl: 'users/profile.html',
resolve: {
auth: function($state) {
firebase.auth().onAuthStateChanged(function(user) {
if (user == null) {
$state.go('home');
console.log("In auth but user NOT valid");
} else {
console.log("In auth and user valid");
}
});
},
profile: function(Users) {
firebase.auth().onAuthStateChanged(function(user) {
if (user != null) {
console.log("In profile and user valid");
return Users.getProfile(user.uid).$loaded();
} else {
console.log("In profile but user NOT valid");
}
});
}
}
});
CONSOLE.LOG :
주요 응용 프로그램에서angular.module('angularfireSlackApp')
// Provides a means of retrieving User data or list of all Users
.factory('Users', function($firebaseArray, $firebaseObject) {
// Provides a means of retrieving User data or list of all Users
// Create a reference to users that can be used to retrieve an array of users
var usersRef = firebase.database().ref("users");
users = $firebaseArray(usersRef);
var Users = {
// Returns a firebase object of a specific user's profile
getProfile: function(uid) {
return $firebaseObject(usersRef.child(uid));
},
// Returns the display name of a specific user
getDisplayName: function(uid) {
return users.$getRecord(uid).displayName;
},
// Returns the Gravatar url that corresponds to the user
getGravatar: function(uid) {
return 'www.gravatar.com/avatar/' + users.$getRecord(uid).emailHash;
},
// Returns a firebase array of users
all: users
};
return Users;
});
프로필 상태
어떤 이유인지 나는 profileCtrl.profile이라는 오류가 발생합니다. $ save()는 함수가 아닙니다. profileCtrl.profile 객체가 합법적이며 $ save()를 적절히 사용하고 있지만 다른 문제가 무엇인지 파악할 수 없다는 것을 알고 있습니다.
내 직감은 내가 간단한 것을 놓치고 있다는 것이지만 AngularJS와 Firebase에 새로운 것이므로 놀라지 않을 것입니다.
당신은'firebaseObject' 나'FirebaseArray'에서만'$ save'를 호출 할 수 있습니다. 그것들 중 누구라도'profileCtrl.profile'입니다. 자세한 내용은 다음을 참조하십시오. https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-save – Adi
어떤 일이 벌어지고 있는지 더 잘 알 수 있도록 코드를 추가했습니다. 'profile'은 사용자 컨트롤러를 통해 firebaseObject를 반환하는 resolve 메소드의 일부인 종속성입니다. 나는 디버깅을했고, firebaseObject라고 말할 수있는 한 멀리 봤다. –