问题描述
我正在使用 firebase 和 angular 进行测验.在测验结束时,我想使用以下代码将新属性保存到用户对象:
I am working on a quiz using firebase and angular. At the end of the quiz i want to save a new property to the users object using this code:
function saveTopscore() {
var ref = new Firebase(CONSTANTS.FIREBASE_URL + 'users/' + User.user.$id + '/');
var userObject = $firebaseObject(ref);
userObject.topscore = User.totalCorrect;
userObject.$save().then(function(ref) {
console.log("worked");
}, function(error) {
console.log(error);
});
}
它可以工作,但它也会覆盖用户对象的所有属性.因此,该对象用于保存名称、用户名、电子邮件、密码等,但是当我推送最高分时,它成为该对象的唯一属性.为什么?
And it works, but it also overwrite all the properties of the user object. So the object used to hold like a name, username, email, password etc, but when i push the topscore it becomes the only property of the object. Why?
推荐答案
你为什么在这里使用 $firebaseObject
?这仅在您将其三向绑定到 Angular 视图时才有用.如果您使用的是常规 JavaScript 代码,只需坚持使用 Firebase 的常规 JavaScript SDK.
Why are you using $firebaseObject
here? That is only useful if you three-way bind it to an Angular view. If you're in regular JavaScript code, just stick to Firebase's regular JavaScript SDK.
在这种情况下,只需:
ref.update({ topscore: User.totalCorrect });
这篇关于保存新属性会覆盖 firebase 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!