简体   繁体   English

ROR-coffeescript导致“ Uncaught ReferenceError”

[英]ROR - coffeescript causing “Uncaught ReferenceError”

The error I am getting is puzzling as I copied some working code from one controller/views /javascript making name changes and took out lines to get an initial working version. 当我从一个控制器/视图/ javascript复制一些工作代码并更改名称并取出行以获取初始工作版本时,出现的错误令人困惑。 The revelvant part of the coffeescript file causing the run time error is below. 下面是导致运行时错误的coffeescript文件的相关部分。

app/assets/javascripts/companies.js.coffee 应用程序/资产/ JavaScript的/ companies.js.coffee

...
calculateResult = (company_id)-> 
  data = $('#x_company_drill_interests').serialize()
  console.log(" -01- in calculateResult ")
  console.log data
  $.ajax
    url:"/companies/#{company_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
       console.log(" -01- in calculateResult - Failure ")
       console.log data
    success: (data,success,xhr)->
       console.log("-01- in calculateResult - SUCCESS ")
       company_listings_block = $ '#x_company_listings_results .infogroup-body'
       head_row = $ '''
<tr>
  <th>Company</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        name = result.display_name
        result_row = $ """
<tr>
  <td>#{name}</td>
</tr>
"""
        table.append result_row
      eval_result_block.html('').append table

Produces the error : 产生错误:

uncaught ReferenceError: head_row is not defined 未捕获的ReferenceError:head_row未定义

Which is caused by the line .append(head_row) . 这是由.append(head_row)行引起的。 If I remove this line I get this error Uncaught ReferenceError: eval_result_block is not defined . 如果删除此行, Uncaught ReferenceError: eval_result_block is not defined出现此错误Uncaught ReferenceError: eval_result_block is not defined

Also for some reason I can't get code to use the Post route. 同样由于某种原因,我无法获得使用Post路由的代码。 This is how I add to amend the routes file. 这就是我添加以修改路由文件的方式。

config/rutes.rb 配置/ rutes.rb

resources :companies, only: [:destroy, :update] do
  member do
    get 'companies_drill_interests'
    match 'projection', via: [:get,:post, :patch]
  end
end

For reference here is the working code I copied from 供参考的是我从中复制的工作代码

calculateResult = (drill_id)->
  console.log("  -001- in calculate results")
  data = $('#x_evaluation_assumption_params').serialize()
  $.ajax
    url:"/drills/#{drill_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
      console.log("     -001- in Calculate Result - Failure ")
      console.log data
    success: (data,success,xhr)->
      # console.log("     -001-     print data from call")
      eval_result_block = $ '#x_id_evaluation_results .infogroup-body'
      head_row = $ '''
<tr>
  <th>Company</th>
  <th>Price</th>
  <th>Mkt Cap</th>
  <th>Discovery Value</th>
  <th>Target Price</th>
  <th>Leverage</th>
  <th>Risked Lev </th>
  <th>Leverage with CFD's</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        if !(result.listing.option_unlisted) 
          name = result.display_name
          share_price = '$' +
            NumberHelpers.number_with_precision((result.listing.share_price/1000), {separator: '.', precision: 3, delimiter: ','})
          market_capitalisation_mill = '$' +                 
             NumberHelpers.number_with_precision((result.market_capitalisation/1000000), {separator: '.', precision: 1, delimiter: ','}) + 'M'
          discovery_value = '$' + 
            NumberHelpers.number_with_precision(result.discovery_value_total, {separator: '.', precision: 0, delimiter: ','})
          discovery_value_per_share = '$' + 
            NumberHelpers.number_with_precision((result.target_share_price), {separator: '.', precision: 2, delimiter: ','})
          leverage = 
        NumberHelpers.number_with_precision(result.leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
      risked_leverage =  
            NumberHelpers.number_with_precision(result.risked_leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
          leverage_with_CFD = 
            NumberHelpers.number_with_precision(result.leverage_with_CFD, {separator: '.', precision: 0, delimiter: ','}) + '%'
          result_row = $ """
<tr>
  <td>#{name}</td>
  <td>#{share_price}</td>
  <td>#{market_capitalisation_mill}</td>
  <td>#{discovery_value}</td>
  <td>#{discovery_value_per_share}</td>
  <td>#{leverage}</td>
  <td>#{risked_leverage}</td>
  <td>#{leverage_with_CFD}</td>
</tr>
"""
        table.append result_row
      eval_result_block.html('').append table

In the first code, eval_result_block is not defined before it is used. 在第一个代码中,在使用eval_result_block之前未对其进行定义。 About result_row , in the first part of the code in it is into a for loop, and in the other part of the code it is outside this for loop because of risked_leverage has a different indent, 4 spaces shorter. 关于result_row ,在其中的代码的第一部分进入for循环,而在代码的另一部分,由于risked_leverage具有不同的缩进,因此在该for循环之外,缩短了4个空格。 That means it is outside of the for loop, so, that is why result_row is outside too. 这意味着它在for循环之外,因此,这就是为什么result_row外部的原因。

In coffeescript, indent matter. 在coffeescript中,缩进事项。 If you put 3 spaces instead of 2, generated code will be different. 如果您将3个空格而不是2个空格,则生成的代码将有所不同。 Look at your code: 查看您的代码:

failure: (data,success,xhr)->
   console.log(" -01- in calculateResult - Failure ")
   console.log data
success: (data,success,xhr)->
   console.log("-01- in calculateResult - SUCCESS ")
   company_listings_block = $ '#x_company_listings_results .infogroup-body'
   head_row = $ '''

If different of 如果不同

failure: (data,success,xhr)->
  console.log(" -01- in calculateResult - Failure ")
  console.log data
success: (data,success,xhr)->
  console.log("-01- in calculateResult - SUCCESS ")
  company_listings_block = $ '#x_company_listings_results .infogroup-body'
  head_row = $ '''

Because in the first case there 3 spaces, and in hte second one there is 2 spaces. 因为在第一种情况下有3个空格,而在第二种情况下有2个空格。

Here your code with a correct indent. 在这里,您的代码带有正确的缩进。 and by reading your code I'am thinking that result_row and table.append result_row have to be in the for loop. 通过阅读您的代码,我认为result_rowtable.append result_row必须位于for循环中。

calculateResult = (company_id)-> 
  data = $('#x_company_drill_interests').serialize()
  console.log(" -01- in calculateResult ")
  console.log data
  $.ajax
    url:"/companies/#{company_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
      console.log(" -01- in calculateResult - Failure ")
      console.log data
    success: (data,success,xhr)->
      console.log("-01- in calculateResult - SUCCESS ")
      company_listings_block = $ '#x_company_listings_results .infogroup-body'
      head_row = $ '''
<tr>
  <th>Company</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        name = result.display_name
        result_row = $ """
<tr>
  <td>#{name}</td>
</tr>
"""
        table.append result_row
      company_listings_block.html('').append table

You can try to compile each part of code into javascript, it will help you to understand what happen. 您可以尝试将代码的每个部分编译为javascript,这将帮助您了解发生了什么。 Coffee compiler 咖啡编译器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM