【问题标题】:Returning newly created key after .push firebase在 .push firebase 之后返回新创建的密钥
【发布时间】:2016-12-05 21:14:44
【问题描述】:
我是 Firebase 的新手,但我需要在将 prop 推送到 Firebase 后 60 秒更新它。 .push() 成功后如何获取 Firebase 创建的动态密钥?
function storeData(val){
$scope.ref = new Firebase('...')
$scope.ref.push({'va1': val, 'status': 'active' });
//after push give me back the newly created key string for that item only
}
【问题讨论】:
标签:
angularjs
json
firebase
firebase-realtime-database
【解决方案1】:
传递push()末尾的key属性。
function storeData(val){
const dataRef = new Firebase('...')
const key = dataRef.push({'va1': val, 'status': 'active' }).key;
}
// Before returning the key value, it pushes the data to the database.
【解决方案2】:
我想通了。我发现这很有魅力
$scope.ref.limitToLast(1).on("child_added", function(snapshot) {
console.log(snapshot.key());
})