'how can i delete multiple paths with firebase cloud functions
I want to delete data older than 15 days. Can I query and delete multiple directories at the same time?
The database structure is like this. There are multiple languages and types.
{
"hr" : {
"classic" : {
"finish" : {
"-MuJD4oAlQ42TkdfKW5M" : {
"gid" : "LlAHMyj",
"ts" : 6.652890289073215E8
}
}
},
"duel" : {
"finish" : {
"-MuJD4o9LONeayt4ACMC" : {
"gid" : "rfePU2x",
"ts" : 6.652890289073215E8
}
}
},
"target" : {
"finish" : {
"-MuJD4o9LONeayt4ACMD" : {
"gid" : "FBz4H_4",
"ts" : 6.652890289073215E8
}
}
}
},
"nl" : {
"target" : {
"finish" : {
"-MuJD4oAlQ42TkdfKW5L" : {
"gid" : "sv3pRfC",
"ts" : 6.652890289073215E8
}
}
}
}
}
I tried to do it with the code below but it always returns null.
exports.deleteOldData = function (snap, context) {
const end = new Date(new Date().setDate(new Date().getDate() - 15));
const endDate = toSwiftDate(end);
database.ref("/matchTour/{language}/{type}/finish/").orderByChild("ts").endAt(endDate).transaction(function (info) {
if (info === null) return info;
console.log("matchTour 3", JSON.stringify(info.val()));
return null;
}).catch((error) => {
console.log("error);
});
};
Solution 1:[1]
You can't run a transaction on a query. In fact, from looking at the reference documentation for Query this shouldn't even compile.
This should be closer:
exports.deleteOldData = function (snap, context) {
const end = new Date(new Date().setDate(new Date().getDate() - 15));
const endDate = toSwiftDate(end);
const ref = database.ref("/matchTour/{language}/{type}/finish/");
return ref
.orderByChild("ts").endAt(endDate)
.once("value")
.then((results) => {
let updates = {};
results.forEach((snapshot) => {
updates[snapshot.key] = null;
});
return ref.update(updates);
}).catch((error) => {
console.error(error);
});
};
The above reads the results for the query from the data, performs a single multi-path update to delete all of them, and ensures the the promises returned by the asynchronous read and write bubble up, so that your Functions container doesn't get terminated prematurely.
That leaves the question of what language and type are in database.ref("/matchTour/{language}/{type}/finish/"). If there are meant to be variables that come from the context, you'll want to use backticks for this string:
database.ref(`/matchTour/{language}/{type}/finish/`)
All in all there are quite a few mistakes in this code that make me think you may not yet be too comfortable with writing server-side JavaScript. If that is the case, writing Cloud Functions is not the easiest way to get started, and I recommend first writing the same code in a client-side JavaScript environment, like a local node.js script, or even a web page.
Reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer are great starting points.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
