您好,欢迎访问一九零五行业门户网

Firebase相关操作的实例代码

今天需要对 firebase 增加删除功能,代码精简如下:
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
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; // null
   var 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();  // true
   var b = snapshot.child(name).exists(); // true
   var c = snapshot.child(name/first).exists(); // true
   var 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(); // true
   var b = snapshot.child(name).haschildren(); // true
   var 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
>> orderbychild
var ref = admin.database().ref(dinosaurs);
ref.orderbychild(height).on(child_added, function(snapshot) {
 console.log(snapshot.key + was + snapshot.val().height + m tall);
});
>> orderbykey
var ref = admin.database().ref(dinosaurs);
ref.orderbykey().on(child_added, function(snapshot) {
 console.log(snapshot.key);
});
>> orderbyvalue
var scoresref = admin.database().ref(scores);
scoresref.orderbyvalue().limittolast(3).on(value, function(snapshot) {
 snapshot.foreach(function(data) {
   console.log(the + data.key + score is + data.val());
 });
});
>> 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);
});
>> equalto
// find all dinosaurs whose height is exactly 25 meters.var ref = admin.database().ref(dinosaurs);
ref.orderbychild(height).equalto(25).on(child_added, function(snapshot) {
 console.log(snapshot.key);
});
>> isequal
var rootref = admin.database().ref();var usersref = rootref.child(users);
usersref.isequal(rootref);  // falseusersref.isequal(rootref.child(users));  // trueusersref.parent.isequal(rootref);  // true
var rootref = admin.database().ref();var usersref = rootref.child(users);var usersquery = usersref.limittolast(10);
usersquery.isequal(usersref);  // falseusersquery.isequal(usersref.limittolast(10));  // trueusersquery.isequal(rootref.limittolast(10));  // falseusersquery.isequal(usersref.orderbykey().limittolast(10));  // false
>> tostring
// calling tostring() on a root firebase reference returns the url where its// data is stored within the database:var rootref = admin.database().ref();var rooturl = rootref.tostring();// rooturl === https://sample-app.firebaseio.com/.// calling tostring() at a deeper firebase reference returns the url of that// deep path within the database:var adaref = rootref.child('users/ada');var adaurl = adaref.tostring();// adaurl === https://sample-app.firebaseio.com/users/ada.
以上就是firebase相关操作的实例代码的详细内容。
其它类似信息

推荐信息