【问题标题】:Get list of related values Angularfire 4 Ionic 3获取相关值列表Angularfire 4 Ionic 3
【发布时间】:2017-10-17 20:48:06
【问题描述】:

我正在努力计算 ionic 中两次之间的差异。

我正在使用 AngularFire,我的树看起来像这样:

{
    "users": {
        "name": {
            "17": {
                "10": {
                    "2017": {
                        "-Kwfm1k9_A74PzlmijUJ": {
                            "date": "17/10/2017",
                            "hora": "17:20:58",
                            "status": "In"
                        },
                        "-Kwfm8wEJ8Oob4YFvNNu": {
                            "date": "17/10/2017",
                            "hora": "17:21:27",
                            "status": "Out"
                        },
                        "-KwfoKkPJMt2g8AQNmxq": {
                            "date": "17/10/2017",
                            "hora": "17:31:00",
                            "status": "In"
                        },
                        "-Kwfp0BOAGnM-2_MfziP": {
                            "date": "17/10/2017",
                            "hora": "17:33:58",
                            "status": "Out"
                        },
                        "-KwfqW5XKpUNedda4rZz": {
                            "date": "17/10/2017",
                            "hora": "17:40:31",
                            "status": "In"
                        },
                        "-Kwg0pQDlI3FMV3BPNaa": {
                            "date": "17/10/2017",
                            "hora": "18:29:58",
                            "status": "Out"
                        }
                    }
                }
            }
        }
    }
}

我想知道第一个和第二个,第三个和第四个,第五个和第六个之间的区别,记住它们是通过标签“状态”相关的。

First In - Out = difference
Second In - Out = difference
Third In - Out = difference

总是在输入和输出之间做不同的事情。

In's 用于当有人进入房间时。 Out's 用于当一个人从房间里出来的时候。我想记录一个人在房间里度过的时间。

所以结果是:

"17:20:58" - "17:21:27" = 00:01:29
"17:31:00" - "17:33:58" = 00:02:58
"17:40:31" - "18:29:58" = 00:49:27

您对重构此代码以使其正常工作有什么建议吗?

记住我正在使用 Ionic 3 和 AngularFire4

感谢您的帮助。

【问题讨论】:

  • IN 和 Out 是如何相互关联的?你能描述一下用例吗?
  • 谢谢伙计。编辑了更好的描述。检查一下:Out 用于当一个人走出房间时。我想记录一个人在房间里度过的时间。
  • 好的,明白了。正在寻找答案。

标签: firebase ionic-framework ionic3 angularfire


【解决方案1】:

为了实现您的目标,我建议您重组数据。目前,IN 和 OUT 之间没有真正的关系。

一种可能的方法是创建一些包含 checkIn 和 checkOut 的节点。我们称之为session。每次用户签入新会话时,都会创建一个会话,而每次用户签出一个会话时,就会关闭。

您的结构可能类似于:

"sessions": {
  "uid": {  // or name or whatever
    "17-10-2017": { // not sure if you need the date in the structure, but if you need it make a single node like this
      "-Kwfm1k9_A74PzlmijUJ": { // this is a session create it on each check in
        "checkin": 1508354574, // timestamp
        "checkout": 1508354584
      },
    }
  }
}

这是一个代码示例(只是为了让您了解它的外观):

var ref = firebase.database().ref("sessions/uid"); // uid must be dynamic

// checkin
ref.push({
  checkin: firebase.database.ServerValue.TIMESTAMP // make sure to use the servers timestamp
}); // creates a new session with a pushkey as id

// checkout
ref.child('sessionId').update({
  checkout: firebase.database.ServerValue.TIMESTAMP
});

// stats
ref.child('sessionId').once('value', function(snap) {
  var session = snap.val();
  var difference = session.checkout - session.checkin;

  // now you have the difference in milliseconds or seconds depending on your timestamp format. this can be formatted whatever unit you want e.g. minutes, hours...
});

希望这能给你一个想法,如何做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2019-09-06
    • 2021-12-19
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多