ホームページ  >  記事  >  ウェブフロントエンド  >  Firebase関連の操作とコード例

Firebase関連の操作とコード例

巴扎黑
巴扎黑オリジナル
2017-06-27 09:15:111294ブラウズ

今日は 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: });

管理者.database.DataSnapshot

>> キー

// 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"
  });
> 子

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

// 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
  });
& gt;>子供たち

admin.database.Query

>startAt、endAt

// 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();
  });
});

>limitToFirst、limitToLast

// 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
  });

... ...

以上がFirebase関連の操作とコード例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。