更新数据,如果存在于Firebase中 [英] Update data if exist in firebase
问题描述
var ref = firebase.database()。ref() .child( '用户');
var refUserId = firebase.database()。ref()。child('users')。orderByChild('id')。equalTo(Auth。$ getAuth()。uid);
refUserId.once('value',function(snapshot){
console.log(snapshot);
if(snapshot.exists()){
snapshot.ref() .update(vm.user_infos);
} else {
ref.push({
player:vm.user_infos.player,
id:vm.user_infos.id
),函数(错误){
console.log(error);
})
}
});
推送工作正常,但更新没有。
snapshot.ref不是函数
在快照()日志控制台中:
我也是这样试过的:如果(snapshot.exists()){
refUserId.update({
player:vm.user_infos。 player,
id:vm.user_infos.id
},function(error){
console.log(error);
})
$ c $结果:
lockquote
refUserId.update不是功能
用户结构
是一个对象 - 不是函数。
$ b 第二个是快照引用 users
路径,所以你应该检查一个用户匹配您的查询是这样的:
$ b
var ref = firebase.database()。ref()。孩子( '用户');
var refUserId = ref.orderByChild('id')。equalTo(Auth。$ getAuth()。uid);
refUserId.once('value',function(snapshot){
if(snapshot.hasChildren()){
snapshot.forEach(function(child){
child.ref .update(vm.user_infos);
});
} else {
snapshot.ref.push({
player:vm.user_infos.player,
id :vm.user_infos.id
});
}
});
如果您想知道何时更新
或 push
已完成,您可以使用promises:
refUserId
.once('value')
.then(function(snapshot){
if(snapshot.hasChildren()){
return snapshot.forEach (function(child){
child.ref.update(vm.user_infos);
});
} else {
return snapshot.ref.push({
player:vm.user_infos.player,
id:vm.user_infos.id
});
}
})
.then(function(){
console.log('update / push done');
})
.catch(function(error){
console.log(error);
}) ;
I'm trying to update a data if it exists
var ref = firebase.database().ref().child('users');
var refUserId = firebase.database().ref().child('users').orderByChild('id').equalTo(Auth.$getAuth().uid);
refUserId.once('value', function (snapshot) {
console.log(snapshot);
if (snapshot.exists()) {
snapshot.ref().update(vm.user_infos);
} else {
ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
}, function(error) {
console.log(error);
})
}
});
Push is working fine, but the update did not.
snapshot.ref is not a function
In the snapshot () log console:
I tried this way too:
if (snapshot.exists()) {
refUserId.update({
player: vm.user_infos.player,
id: vm.user_infos.id
}, function(error) {
console.log(error);
})
Result:
refUserId.update is not a function
User structure
The first problem is that the snapshot's ref
property is an object - not a function.
The second is that the snapshot refers to the users
path, so you should check for a user that matches your query like this:
var ref = firebase.database().ref().child('users');
var refUserId = ref.orderByChild('id').equalTo(Auth.$getAuth().uid);
refUserId.once('value', function (snapshot) {
if (snapshot.hasChildren()) {
snapshot.forEach(function (child) {
child.ref.update(vm.user_infos);
});
} else {
snapshot.ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
});
}
});
And if you want to know when the update
or push
has completed, you could use promises:
refUserId
.once('value')
.then(function (snapshot) {
if (snapshot.hasChildren()) {
return snapshot.forEach(function (child) {
child.ref.update(vm.user_infos);
});
} else {
return snapshot.ref.push({
player: vm.user_infos.player,
id: vm.user_infos.id
});
}
})
.then(function () {
console.log('update/push done');
})
.catch(function (error) {
console.log(error);
});
这篇关于更新数据,如果存在于Firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!