Home Web Front-end JS Tutorial Firebase related operations and code examples

Firebase related operations and code examples

Jun 27, 2017 am 09:15 AM
firebase code Original operate Related

Today we need to add the deletion function to Firebase. The code is simplified as follows:

 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         });
Copy after login

Firebase modification node:

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.');
        }
    });
}
Copy after login

Firebase listening:

var signaturesRef = defaultApp.database().ref('signatures');

signaturesRef.orderByChild("isChecked").equalTo(false).on("child_added", function(snapshot, prevChildKey) {// TODO: });
Copy after login

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"
  });
Copy after login

>> child

var rootRef = admin.database().ref();
rootRef.once("value")
  .then(function(snapshot) {var key = snapshot.key; // nullvar childKey = snapshot.child("users/ada").key; // "ada"
  });
Copy after login

>> 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
  });
Copy after login

>> 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();
  });
});
Copy after login

>> 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
  });
Copy after login

>> 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
  });
Copy after login

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);
});
Copy after login

>> ; 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);
});
Copy after login

……

The above is the detailed content of Firebase related operations and code examples. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do if the blue screen code 0x0000001 occurs

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm usage tutorial: guide you in detail to run the operation

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

What is sudo and why is it important?

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

GE universal remote codes program on any device

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

Linux Deploy operation steps and precautions

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

What to do if you forget to press F2 for win10 boot password

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

Huawei Mate60 Pro screenshot operation steps sharing

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing

See all articles