1

我有一个像这样运行得很好的查询。它返回正确的对象和所有内容。

    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: 'previous_300_minutes'
    });

但我想做的是做这样的事情。看看我如何使用时间变量并将其插入到“时间范围”的查询中?但是它总是返回未定义的。

if (this.value == "1"){
      var time = 'previous_300_minutes'
    } else {
      var time = 'previous_30_minutes'
    }
    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: time
    });

这总是返回未定义。我认为它是因为我不能像这样将时间字符串变量插入到查询 JSON 中。如何插入它以使其格式正确?

4

2 回答 2

3

试试这样:

var time;
if (this.value == "1"){
  time = 'previous_300_minutes'
} else {
  time = 'previous_30_minutes'
}
var all = new Keen.Query("extraction", {
  eventCollection: "android-sample-button-clicks",
  timeframe: time
});

或者像这样:

var time = (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes';
var all = new Keen.Query("extraction", {
  eventCollection: "android-sample-button-clicks",
  timeframe: time
});

甚至像这样:

var all = new Keen.Query("extraction", {
  eventCollection: "android-sample-button-clicks",
  timeframe: (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes'
});
于 2015-02-15T19:36:08.730 回答
0

因为它是异步的。像这样重写

if (this.value == "1"){
      var time = 'previous_300_minutes'
    var all = new Keen.Query("extraction", {
    eventCollection: "android-sample-button-clicks",
    timeframe: time
    });
   } else {
      var time = 'previous_30_minutes'
    var all = new Keen.Query("extraction", {
    eventCollection: "android-sample-button-clicks",
    timeframe: time
    });
    }
于 2015-02-15T19:36:22.153 回答