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 });
>> numChildren
#
// 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); });
>> startAt, endAt
// 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); });
>> ; limitToFirst, limitToLast
rrreee
#
####......##### ###以上是Firebase 相關操作及程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

藍屏代碼0x0000001怎麼辦藍屏錯誤是電腦系統或硬體出現問題時的一種警告機制,代碼0x0000001通常表示出現了硬體或驅動程式故障。當使用者在使用電腦時突然遇到藍色畫面錯誤,可能會感到驚慌失措。幸運的是,大多數藍色畫面錯誤都可以透過一些簡單的步驟來排除和處理。本文將為讀者介紹一些解決藍屏錯誤代碼0x0000001的方法。首先,當遇到藍色畫面錯誤時,我們可以嘗試重

PyCharm是一款非常受歡迎的Python整合開發環境(IDE),它提供了豐富的功能和工具,使得Python開發變得更有效率和便利。本文將為大家介紹PyCharm的基本操作方法,並提供具體的程式碼範例,幫助讀者快速入門並熟練操作工具。 1.下載安裝PyCharm首先,我們需要前往PyCharm官網(https://www.jetbrains.com/pyc

sudo(超級使用者執行)是Linux和Unix系統中的關鍵指令,允許一般使用者以root權限執行特定指令。 sudo的功能主要體現在以下幾個方面:提供權限控制:sudo透過授權使用者以臨時方式取得超級使用者權限,從而實現了對系統資源和敏感操作的嚴格控制。普通用戶只能在需要時透過sudo獲得臨時的特權,而不需要一直以超級用戶登入。提升安全性:透過使用sudo,可以避免在常規操作中使用root帳號。使用root帳戶進行所有操作可能會導致意外的系統損壞,因為任何錯誤或不小心的操作都將具有完全的權限。而

如果您需要遠端編程任何設備,這篇文章會為您帶來幫助。我們將分享編程任何設備的頂級GE通用遠端代碼。通用電氣的遙控器是什麼? GEUniversalRemote是一款遙控器,可用於控制多個設備,如智慧電視、LG、Vizio、索尼、藍光、DVD、DVR、Roku、AppleTV、串流媒體播放器等。 GEUniversal遙控器有各種型號,具有不同的功能和功能。 GEUniversalRemote最多可以控制四台設備。頂級通用遙控器代碼,可在任何裝置上編程GE遙控器配備一組代碼,使其能夠與不同設備配合。您可

LinuxDeploy的操作步驟及注意事項LinuxDeploy是一款強大的工具,可協助使用者在Android裝置上快速部署各種Linux發行版,讓使用者在行動裝置上體驗完整的Linux系統。本文將詳細介紹LinuxDeploy的操作步驟以及注意事項,同時提供具體的程式碼範例,幫助讀者更好地使用此工具。操作步驟:安裝LinuxDeploy:首先在

想必很多的用戶家裡都有那麼幾台不用的電腦,因為長時間不用完全忘了開機密碼,於是想知道一下,忘記密碼要怎麼操作呢?那就一起來看看吧。 win10開機密碼忘記按F2怎麼操作1、按下電腦的電源鍵,然後開機時按下F2(不同電腦品牌進入bios的按鍵也不同)。 2.在bios介面中,找到security選項(不同品牌電腦的位置可能有所不同)。一般都在頂部的設定選單中。 3.然後找到SupervisorPassword選項並且點選。 4.這時候用戶就可以看到自己的密碼了,同時找到旁邊的Enabled切換為Dis

身為一名程式設計師,對於能夠簡化程式設計體驗的工具,我感到非常興奮。借助人工智慧工具的幫助,我們可以產生演示程式碼,並根據需求進行必要的修改。在VisualStudioCode中新引入的Copilot工具讓我們能夠創建具有自然語言聊天互動的AI生成程式碼。透過解釋功能,我們可以更好地理解現有程式碼的含義。如何使用Copilot產生程式碼?要開始,我們首先需要取得最新的PowerPlatformTools擴充。要實現這一點,你需要進入擴充頁面,搜尋“PowerPlatformTool”,然後點擊Install按鈕

隨著智慧型手機的普及,螢幕截圖功能成為日常使用手機的必備技能之一。華為Mate60Pro作為華為公司的旗艦手機之一,其截圖功能自然也備受用戶關注。今天,我們就來分享華為Mate60Pro手機的截圖操作步驟,讓大家能夠更方便地進行截圖操作。首先,華為Mate60Pro手機提供了多種截圖方式,可以依照個人習慣選擇適合自己的方式來操作。以下詳細介紹幾種常用的截
