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

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)