0

当我将本地主机和同一个应用程序上传到 Heroku 时,我感到很困惑。

我通过控制器方法即时渲染 JSON,并使用 AJAX 请求在 Javascript 中获取它。

在本地主机上,它运行良好。但是,当我上传到 Heroku 时,它根本不起作用。不知道为什么会这样。这是否与 Heroku 访问我的模型的方式和我的本地主机的方式之间的差异有关?

以下是来自同一时间戳的两个调试输出:

在职的:

之前是 106.944877,现在是 0,增量为 -84.07055608611111

不工作:

以前是 0,现在是 0,增量为 0

这是代码:

控制器方法#1:

# present data to grab for view work
# get/user_data
def user_inst_data
timestamp = params[:time_now]
user_id = params[:user_id]

before = fetch_data_at_time(user_id, timestamp)
after = fetch_data_at_time(user_id, (timestamp.to_i+3600))

response = Hash.new

response['before'] = before
response['after'] = after

render json: response.to_json
end

控制器方法#2:

def fetch_data_at_time(user_id, time_at)
  # convert time to object
  timestamp = Time.at(time_at.to_i)
  # break down timestamp right now
  year = timestamp.year
  month = timestamp.month
  day = timestamp.day
  hour = timestamp.hour+1 #we're 1-indexed

  # find data
  response = UserData.where(:user_id=>user_id, :year=>year, :month=>month, :day=>day, :hour=>hour).select('power')

  return response
end

的JavaScript:

function real_time_update(){
  //this function will fake "real-time" updates by grabbing simulated data from the DB
  //continue to update component every 5 seconds
  setTimeout(real_time_update,5000);
  var time = Math.round(((new Date()).getTime())/1000);

  var targetURL = "get/user_data?time_now="+time+"&user_id="+<%= @user.id %>;

  var power_before = 0;
  var power_after = 0;

  // GET data with AJAX request, set function for successful return
  $.get(targetURL, function(data){
    power_before = data.before[0].power;
    power_after = data.after[0].power;

    // after getting data before and after, find increment between them, delta-naught
    var delta = power_after - power_before;

    // scale before power by a percentage based on how much time has elapsed since last hour
    var current_time = new Date();
    var minutes_passed = (current_time.getMinutes())*60;
    var seconds_passed = minutes_passed + (current_time.getSeconds());
    var percent_hour = seconds_passed/3600;

    // calculate increment
    var delta_change = delta*percent_hour;

    // DEBUG----
    console.log("Before it was "+power_before+", now it is "+power_after+" with increments of "+delta_change)

    //add to power_before and update html component
    var power_update = power_before + delta_change;

    // redraw component
    drawChart(power_update);
  })
}
4

0 回答 0