Javascript Firebase crontab清理
我正在尝试删除FirebaseDB中插入的地理位置,如下所示:Javascript Firebase crontab清理,javascript,android,firebase,google-cloud-functions,Javascript,Android,Firebase,Google Cloud Functions,我正在尝试删除FirebaseDB中插入的地理位置,如下所示: map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { @Override public void onMapLongClick(final LatLng latLng) { dbRef.child("hora").setValue(ServerValue.TIMESTAMP);
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng latLng) {
dbRef.child("hora").setValue(ServerValue.TIMESTAMP); //Executes TIMESTAMP function in firebase server and stores that value
dbRef.child("hora").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
hora = String.valueOf(dataSnapshot.getValue());
geoFire.setLocation(hora, new GeoLocation(latLng.latitude,latLng.longitude), new GeoFire.CompletionListener() {
@Override //Save geolocation with timestamp in seconds
public void onComplete(String key, DatabaseError error) {
if (error != null) {
Log.v("Informe","There was an error saving the location to GeoFire: " + error);
} else {
Log.v("Informe","Location saved on server successfully!");
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
{
"geofire" : {
"1515692377982" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
},
"1515692378159" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
}
},
"hora" : 1515692378159
}
在数据库中,如下所示:
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng latLng) {
dbRef.child("hora").setValue(ServerValue.TIMESTAMP); //Executes TIMESTAMP function in firebase server and stores that value
dbRef.child("hora").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
hora = String.valueOf(dataSnapshot.getValue());
geoFire.setLocation(hora, new GeoLocation(latLng.latitude,latLng.longitude), new GeoFire.CompletionListener() {
@Override //Save geolocation with timestamp in seconds
public void onComplete(String key, DatabaseError error) {
if (error != null) {
Log.v("Informe","There was an error saving the location to GeoFire: " + error);
} else {
Log.v("Informe","Location saved on server successfully!");
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
{
"geofire" : {
"1515692377982" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
},
"1515692378159" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
}
},
"hora" : 1515692378159
}
(我不知道为什么只需长按一下就插入了两次)
我已经搜索过了,我知道我必须使用云函数,但我必须了解JS,我不太了解它,所以,可能有人想帮我制作一个函数,用于删除firebase中已存在超过1小时的数据,例如
我知道这一定是由crontab和HTTPS触发的,但我需要首先检查函数是否工作。
我发现其中一些链接对此很有意思:
我认为这应该是这两个链接的混合。最后,我让它工作起来了,所以如果有人需要一个谷歌云函数来删除一小时前的firebase数据(这是由一个带有时间戳的键设置的,看看我的数据模型),现在你可以安排一个crontab每隔一段时间运行一次HTTPS请求
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref().child('geofire'); //Path to the child that contains the items you want to delete
const CUT_OFF_TIME = 3600000; //1 Hour
exports.cleanOldFiles = functions.https.onRequest((req, res) => {
const now = Date.now();
const cutoff = now - CUT_OFF_TIME;
const lastHour = '' + cutoff; //Must be String in order to put it inside .orderByKey().endAt()
const query = ref.orderByKey().endAt(lastHour);
return query.once('value').then(snapshot => {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});