今天需要对 Firebase 增加删除功能,代码精简如下:
1 var admin = require('firebase-admin'); 2 var config = require('./config.json'); 3 4 var defaultAppConfig = { 5 credential: admin.credential.cert(config.firebase.cert), 6 databaseURL: config.firebase.databaseURL 7 }; 8 9 10 var defaultAppName = 'GoPeople-NodeJS-Admin';11 var defaultApp = admin.initializeApp(defaultAppConfig, defaultAppName);12 13 var signaturesRef = defaultApp.database().ref('signatures');14 15 signaturesRef.orderByChild("isChecked").equalTo(true).limitToLast(10).once("value")16 .then(function(snapshot) {17 18 snapshot.forEach(function(childSnapshot) {19 var key = childSnapshot.key;20 var childData = childSnapshot.val();21 22 var now = new Date();23 var date = new Date(childData.date);24 var dayDiff = parseInt((now - date) / (1000 * 60 * 60 * 24)); // day diff25 26 if(dayDiff >30){27 signaturesRef.child(key).remove(function(error) {28 console.log(key);29 console.log(dayDiff);30 console.log(error ? ("Uh oh! " + error) : "Success!");31 });32 }else{33 console.log(key);34 console.log(dayDiff);35 }36 });37 38 });
Firebase 修改节点:
function finishJobSync(jobGuid) {var signaturesRef = defaultApp.database().ref('signatures').child(jobGuid); signaturesRef.update({isChecked: true},function(error) {if (error) { logger.error(error); } else { logger.info('Job ' + jobGuid + ' signature has been synced.'); } }); }
Firebase 监听:
var signaturesRef = defaultApp.database().ref('signatures'); signaturesRef.orderByChild("isChecked").equalTo(false).on("child_added", function(snapshot, prevChildKey) {// TODO: });
admin.database.DataSnapshot
>> key
// Assume we have the following data in the Database:{ "name": {"first": "Ada","last": "Lovelace" } }var ref = admin.database().ref("users/ada"); ref.once("value") .then(function(snapshot) {var key = snapshot.key; // "ada"var childKey = snapshot.child("name/last").key; // "last" });
>> child
var rootRef = admin.database().ref(); rootRef.once("value") .then(function(snapshot) {var key = snapshot.key; // nullvar childKey = snapshot.child("users/ada").key; // "ada" });
>> exists
// Assume we have the following data in the Database:{ "name": {"first": "Ada","last": "Lovelace" } }// Test for the existence of certain keys within a DataSnapshotvar ref = admin.database().ref("users/ada"); ref.once("value") .then(function(snapshot) {var a = snapshot.exists(); // truevar b = snapshot.child("name").exists(); // truevar c = snapshot.child("name/first").exists(); // truevar d = snapshot.child("name/middle").exists(); // false });
>> foreach
// Assume we have the following data in the Database:{ "users": {"ada": { "first": "Ada", "last": "Lovelace"},"alan": { "first": "Alan", "last": "Turing"} } }// Loop through users in order with the forEach() method. The callback// provided to forEach() will be called synchronously with a DataSnapshot// for each child:var query = admin.database().ref("users").orderByKey(); query.once("value") .then(function(snapshot) { snapshot.forEach(function(childSnapshot) { // key will be "ada" the first time and "alan" the second time var key = childSnapshot.key; // childData will be the actual contents of the child var childData = childSnapshot.val(); }); });
>> hasChildren
// Assume we have the following data in the Database:{ "name": {"first": "Ada","last": "Lovelace" } }var ref = admin.database().ref("users/ada"); ref.once("value") .then(function(snapshot) {var a = snapshot.hasChildren(); // truevar b = snapshot.child("name").hasChildren(); // truevar c = snapshot.child("name/first").hasChildren(); // false });
>> numChildren
// Assume we have the following data in the Database:{ "name": {"first": "Ada","last": "Lovelace" } }var ref = admin.database().ref("users/ada"); ref.once("value") .then(function(snapshot) {var a = snapshot.numChildren(); // 1 ("name")var b = snapshot.child("name").numChildren(); // 2 ("first", "last")var c = snapshot.child("name/first").numChildren(); // 0 });
admin.database.Query
>> startAt, endAt
// Find all dinosaurs that are at least three meters tall.var ref = admin.database().ref("dinosaurs"); ref.orderByChild("height").startAt(3).on("child_added", function(snapshot) { console.log(snapshot.key) });// Find all dinosaurs whose names come before Pterodactyl lexicographically.var ref = admin.database().ref("dinosaurs"); ref.orderByKey().endAt("pterodactyl").on("child_added", function(snapshot) { console.log(snapshot.key); });
>> limitToFirst, limitToLast
// Find the two shortest dinosaurs.var ref = admin.database().ref("dinosaurs"); ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) { // This will be called exactly two times (unless there are less than two // dinosaurs in the Database). // It will also get fired again if one of the first two dinosaurs is // removed from the data set, as a new dinosaur will now be the second // shortest. console.log(snapshot.key); });// Find the two heaviest dinosaurs.var ref = admin.database().ref("dinosaurs"); ref.orderByChild("weight").limitToLast(2).on("child_added", function(snapshot) { // This callback will be triggered exactly two times, unless there are // fewer than two dinosaurs stored in the Database. It will also get fired // for every new, heavier dinosaur that gets added to the data set. console.log(snapshot.key); });
......
以上是Firebase 相关操作及代码示例的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。 1.Python以简洁语法和丰富库生态着称,适用于数据分析和Web开发。 2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

JavaScript不需要安装,因为它已内置于现代浏览器中。你只需文本编辑器和浏览器即可开始使用。1)在浏览器环境中,通过标签嵌入HTML文件中运行。2)在Node.js环境中,下载并安装Node.js后,通过命令行运行JavaScript文件。

如何在Quartz中提前发送任务通知在使用Quartz定时器进行任务调度时,任务的执行时间是由cron表达式设定的。现�...

在JavaScript中如何获取原型链上函数的参数在JavaScript编程中,理解和操作原型链上的函数参数是常见且重要的任�...

在微信小程序web-view中使用Vue.js动态style位移失效的原因分析在使用Vue.js...

在Tampermonkey中如何对多个链接进行并发GET请求并依次判断返回结果?在Tampermonkey脚本中,我们经常需要对多个链...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。