每次用户点击按钮时,将“时间”存储在Firebase中

Store 'time' in firebase each time user clicks a button

本文关键字:时间 存储 Firebase 用户 按钮      更新时间:2023-09-26

我有一个带有"完成"按钮的待办事项列表。我想在每次用户点击按钮时将时间存储在 Firebase 中。该按钮在一段时间后启用,当用户再次单击该按钮时,我想再次存储时间而不是覆盖现有时间。在我的代码中,时间被覆盖了。

索引.html

<ion-list class = "list">
    <ion-item class = "has-header" ng-repeat="todo in data.todos" class = "item" ng-class="{purchased: todos.status == 'purchased'}">
     {{todo.title}}
      <button  ng-disabled="todo.stopSpam" ng-click="doneItem(todo)">Done
      </button>

应用.js

$scope.doneItem = function(todo) {
  todo.stopSpam = true; 
  todo.timestamp = Date();
  $interval(callAtInterval, 30000);
  function callAtInterval(){
    todo.stopSpam = false;  
  }
};

为了避免覆盖现有数据,每次要在该节点中存储数据时,代码都应创建新的子节点。

给定的引用

是你的根引用

var timestampsRef = ref.child("timestamps");
var newTimestampRef = timestampsRef.push();
newTimestampRef.set({
    stamp: "a timestamp"
});

每次用户按下按钮时调用它将创建一个不同的节点名称,然后将时间戳作为子项添加到每个节点。

  "timestamps": {
    "-QIUEiijasdj923498": {
      "stamp": "a timestamp"
    },
    "-Qnbnajsd093409koi": {
      "stamp": "a timestamp"
    }