今天需要為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中文網其他相關文章!

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。


熱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 英文版
推薦:為Win版本,支援程式碼提示!

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

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器