首页 web前端 js教程 Firebase 相关操作及代码示例

Firebase 相关操作及代码示例

Jun 27, 2017 am 09:15 AM
firebase 代码 原创 操作 相关

今天需要对 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

蓝屏代码0x0000001怎么办 蓝屏代码0x0000001怎么办 Feb 23, 2024 am 08:09 AM

蓝屏代码0x0000001怎么办蓝屏错误是电脑系统或硬件出现问题时的一种警告机制,代码0x0000001通常表示出现了硬件或驱动程序故障。当用户在使用电脑时突然遇到蓝屏错误,可能会感到惊慌和无措。幸运的是,大多数蓝屏错误都可以通过一些简单的步骤进行排除和处理。本文将为读者介绍一些解决蓝屏错误代码0x0000001的方法。首先,当遇到蓝屏错误时,我们可以尝试重

PyCharm使用教程:详细指引你运行操作 PyCharm使用教程:详细指引你运行操作 Feb 26, 2024 pm 05:51 PM

PyCharm是一款非常流行的Python集成开发环境(IDE),它提供了丰富的功能和工具,使得Python开发变得更加高效和便捷。本文将为大家介绍PyCharm的基本操作方法,并提供具体的代码示例,帮助读者快速入门并熟练操作该工具。1.下载和安装PyCharm首先,我们需要前往PyCharm官网(https://www.jetbrains.com/pyc

什么是 sudo,为什么它如此重要? 什么是 sudo,为什么它如此重要? Feb 21, 2024 pm 07:01 PM

sudo(超级用户执行)是Linux和Unix系统中的一个关键命令,允许普通用户以root权限运行特定命令。sudo的功能主要体现在以下几个方面:提供权限控制:sudo通过授权用户以临时方式获取超级用户权限,从而实现了对系统资源和敏感操作的严格控制。普通用户只能在需要时通过sudo获得临时的特权,而不需要一直以超级用户身份登录。提升安全性:通过使用sudo,可以避免在常规操作中使用root账户。使用root账户进行所有操作可能会导致意外的系统损坏,因为任何错误或不小心的操作都将具有完全的权限。而

GE通用远程代码可在任何设备上编程 GE通用远程代码可在任何设备上编程 Mar 02, 2024 pm 01:58 PM

如果您需要远程编程任何设备,这篇文章会给您带来帮助。我们将分享编程任何设备的顶级GE通用远程代码。通用电气的遥控器是什么?GEUniversalRemote是一款遥控器,可用于控制多个设备,如智能电视、LG、Vizio、索尼、蓝光、DVD、DVR、Roku、AppleTV、流媒体播放器等。GEUniversal遥控器有各种型号,具有不同的功能和功能。GEUniversalRemote最多可以控制四台设备。顶级通用遥控器代码,可在任何设备上编程GE遥控器配备一组代码,使其能够与不同设备相配合。您可

Linux Deploy的操作步骤及注意事项 Linux Deploy的操作步骤及注意事项 Mar 14, 2024 pm 03:03 PM

LinuxDeploy的操作步骤及注意事项LinuxDeploy是一款强大的工具,可以帮助用户在Android设备上快速部署各种Linux发行版,让用户能够在移动设备上体验到完整的Linux系统。本文将详细介绍LinuxDeploy的操作步骤以及注意事项,同时提供具体的代码示例,帮助读者更好地使用这一工具。操作步骤:安装LinuxDeploy:首先在

win10开机密码忘记按F2怎么操作 win10开机密码忘记按F2怎么操作 Feb 28, 2024 am 08:31 AM

想必很多的用户家里都有那么几台不用的电脑,因为长时间不用完全忘记了开机密码,于是想要知道一下,忘记密码要怎么操作呢?那就一起来看看吧。win10开机密码忘记按F2怎么操作1、按下电脑的电源键,然后开机时按下F2(不同电脑品牌进入bios的按键也不同)。2、在bios界面中,找到security选项(不同品牌电脑的位置可能有所不同)。一般都在顶部的设置菜单中。3、然后找到SupervisorPassword选项并且点击。4、这时候用户就可以看到自己的密码了,同时找到旁边的Enabled切换为Dis

如何使用Copilot生成代码 如何使用Copilot生成代码 Mar 23, 2024 am 10:41 AM

作为一名程序员,对于能够简化编码体验的工具,我感到非常兴奋。借助人工智能工具的帮助,我们可以生成演示代码,并根据需求进行必要的修改。在VisualStudioCode中新引入的Copilot工具让我们能够创建具有自然语言聊天交互的AI生成代码。通过解释功能,我们可以更好地理解现有代码的含义。如何使用Copilot生成代码?要开始,我们首先需要获得最新的PowerPlatformTools扩展。要实现这一点,你需要进入扩展页面,搜索“PowerPlatformTool”,然后点击Install按钮

华为Mate60 Pro截屏操作步骤分享 华为Mate60 Pro截屏操作步骤分享 Mar 23, 2024 am 11:15 AM

随着智能手机的普及,截屏功能成为日常使用手机的必备技能之一。华为Mate60Pro作为华为公司的旗舰手机之一,其截屏功能自然也备受用户关注。今天,我们就来分享华为Mate60Pro手机的截屏操作步骤,让大家能够更加便捷地进行截屏操作。首先,华为Mate60Pro手机提供了多种截屏方式,可以根据个人习惯选择适合自己的方式进行操作。下面详细介绍几种常用的截

See all articles